How to mask each region in image within boundary

4 次查看(过去 30 天)
I would like to compute some factor inside the a boundary. I have the coordinates of this boundary. I must use a rectangle which each iteration is moving though the image.

回答(2 个)

Gopichandh Danala
Gopichandh Danala 2017-5-13
% lets assume you have the cordinates of the rectangle..
% a..like 4 vertices of the rectangle...
img = imread('peppers.png');
figure, imshow(img,[]), title('Original Image')
% I am selecting vertices of rectangle...
% we need just two diagonal vertices to get four coordinates
[tempverticesX, tempverticesY] = ginput(2);
tempverticesX = round(tempverticesX);
tempverticesY = round(tempverticesY);
% Get a masked Rectangle for the given cords
maskRegion = zeros(size(img));
maskRegion(tempverticesY(1):tempverticesY(2),tempverticesX(1):tempverticesX(2),:) = 1;
figure, imshow(maskRegion,[]), title('Mask Region')
% I am assuming you need to map pixel values not just 1's or 0's
% mappedRegion = zeros(size(img));
mappedRegion = uint8(maskRegion) .* img;
figure, imshow(mappedRegion), title('Mapped Region')
If you have any others doubts ask me.
  2 个评论
amir nemat
amir nemat 2017-5-14
Please see the updated question. You didn't get me question.
Gopichandh Danala
Gopichandh Danala 2017-5-28
编辑:Gopichandh Danala 2017-5-28
amir, as image analyst said your description is not clear, explain clearly the requirement and what you meant by some factor inside the boundary. Do you have a region mask already and you need a factor or you have some rectangle cords and you need get a mask, then find some features (factors)..?
Maybe explain some details..

请先登录,再进行评论。


Image Analyst
Image Analyst 2017-5-13
Use poly2mask(), then multiply the mask by your image or use it as a logical index
mask = poly2mask(x, y, rows, columns);
maskedImage = grayImage; % Initialize
maskedImage(~mask) = 0; % Blacken outside of the mask.
Now do your other processing.
  2 个评论
amir nemat
amir nemat 2017-5-14
Please see the updated question. You didn't get me question. Poly2mask is not working for this question.
Image Analyst
Image Analyst 2017-5-14
It's still not clear what you want. Can you get a native English speaker to describe it? I mean, maybe you want to know about the conv2() function that moves a rectangular filter window across the image. Or maybe you don't want that blue boundary line in the image. Who knows???
And we still don't know what what "factor" you want to compute when you say "would like to compute some factor". How about the mean intensity? How about the standard deviation? How about the entropy? How about the number of roads or houses in the region? Who knows? I certainly don't. You didn't tell us.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by