Visual Media Processing Using Matlab Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Time for action – whiten an area and blacken another

We will again work using my_image.bmp. Let's see if we can write a script that whitens a 30 x 40 rectangular area on the top-left corner of the image and blackens a 40 x 50 rectangular area at the bottom-right corner of the image. We can manage to do so, if we follow these steps:

  1. First, you should open Editor and select New Script. This can also be achieved by using the Ctrl + N shortcut keystroke.
  2. Now, write the first part of the code, which will open the image:
    img = imread('my_image.bmp');
  3. Then you should alter the values of the elements contained in the top-left rectangle to 255. Let's keep the original image so that we can compare it to the final result. This will be achieved using the following line of code:
    img_final = img;
    img_final(1:30,1:40) = 255;
  4. Now, you should assign black values (equal to 0) to the elements contained in the bottom-right rectangle. In order to define the indices of the pixels you want to alter, you must use the maximum number of rows and columns. This can be easily accomplished using the generic keyword end as follows:
    img_final(end-39:end,end-49:end) = 0;
  5. You have finished the altering part. Now, it's time to display the results (both the original image and the final image), like we did in the previous chapter. The code for this will be as follows:
    subplot(1,2,1)
    imshow(img)
    title('Original image')
    subplot(1,2,2)
    imshow(img_final)
    title('Processed image')
  6. Finally, you should save your script. Let's use the name RectangleBrightness (as before, the extension .m will be added by Editor).
  7. To see the result, we should run your script. Go to the Command Window and type the following code:
    >> RectangleBrightness

    The result should be something like the following image:

What just happened?

First of all, congratulations! You just wrote and executed your first script that alters pixel values. The commands used were not something new, but they were all executed as a batch for an image this time, producing the final result you just saw. The method used for the alteration of the pixel values was indexing, since we said that it is preferable than using for loops.

To select the rectangles to be altered, we had to define the top and bottom row indices and the left and right column indices. The top-left rectangle was defined in a rather intuitive manner. We used index 1 for both the top row and the left column. The indices for the bottom row and the right column were set to 30 and 40 respectively.

The tricky part was selecting the indices that should be used for the bottom-right rectangle. Again, we knew the height and width, but we should use it with respect to the height and width of the image. However, altering the width and height values for each new image would be highly impractical. This is why we used the very convenient index keyword end, which denoted the maximum valued index for each dimension. When it is used for rows, it automatically takes the value of the maximum number of rows, and when it is used for columns it takes the maximum number of columns. In our case we used it in both positions, to calculate the proper top row index (end-39) and bottom row index (end), and also to calculate the proper left column index (end-49) and right row index (end).