To expand a selection in an image to include all pixels from x, y by 20 pixels in both axes in MATLAB, you can use the imdilate
function from the Image Processing Toolbox. Here's an example code:
main.m529 chars24 lines
In this code, we first load the image using the imread
function. Then, we define the selection rectangle by specifying its top-left corner (x
and y
coordinates) and its width and height (width
and height
variables, respectively).
Next, we create a binary mask of the selection using the false
function to create a matrix of the same size as the image, and then set the corresponding pixels to true
in the selection rectangle.
To expand the mask, we use the strel
function to create a structuring element of a square shape with a size of 40x40 pixels (20 pixels in each direction), and then apply it to the mask using imdilate
.
Finally, we apply the mask to the original image using element-wise multiplication with bsxfun
. The resulting image, img_selected
, will have all pixels inside the expanded selection set to their original values, and all other pixels set to 0. We can then display the result using the imshow
function.
gistlibby LogSnag