How to extract ROI on grayscale image using edge detection

11 次查看(过去 30 天)
Hello.
I want to extract the white area on the image below (ROI) by using edge detection. I want to do this beacouse I must get very precise mask of white region. The image loks like this:
If I use some of matlab's inbuild function the prolem is that I also get edges outside of the white areas (on the border between black area and gray area).
I also have to close the whole white area to get mask, after properly detected edges...
Any other suggestions to calculate mask of white area on the first image?
Thank you all!!

采纳的回答

Image Analyst
Image Analyst 2016-12-11
This is very easy, or can be. Just threshold. If you have noise then call imfill() and bwareafilt() to fill and extract the largest blob. Otherwise, simply use find(binaryImage, 1, 'first') to get the x location of the edge. Do for every row and then put into polyfit(x,y,1) to get the equation of a line. Then get the fitted coordinates of a perfect line. then compute the residuals (distances of actual edge to fitted edge). If the residual for any line is more than some certain amount, then there is a defect at that line.
Nowhere in what I said is an edge detection filter required.
Basically something like this:
binaryImage = grayImage > someValue;
binaryImage = imfill(binaryImage, 'holes'); % Optional. Delete for speed
binaryImage = bwareafilt(binaryImage, 1); % Optional, if there is noise in the dark left half.
[rows, columns] = size(binaryImage);
for y = 1 : rows
x(y) = find(binaryImage(y, :), 1, 'first');
end
% Switch x and y so y (line/row) is the independent variable.
y = x;
x = 1 : rows
coefficients = polyfit(x, y, 1); % Fit a line
yFitted = polyval(coefficients, x);
% Scan every line looking for deviation from fitted line
residuals = abs(y - yFitted);
% See if there are any big differences - find out what rows they're on.
defectLines = differences > someThreshold;
and so on. That's just off the otp of my head - not tested - so there may be errors but at least it will give you a start.
  4 个评论
jasmine htwe
jasmine htwe 2018-2-28
[rows, columns] = size(binaryImage); for y = 1 : rows x(y) = find(binaryImage(y, :), 1, 'first'); end % Switch x and y so y (line/row) is the independent variable. y = x; x = 1 : rows
When I run this part, I got this error.
Subscripted assignment dimension mismatch.
Error in differences (line 68) x(y)= find(binaryImage(y,:),1,'first');
My image size is (240,270). So, what can I do? plz give me suggestions?

请先登录,再进行评论。

更多回答(3 个)

Bego
Bego 2016-11-17
编辑:Bego 2016-11-17
Hello Andraz!
In order to solve this, you need to separate the black from the grey and from the white. To do so, it is necessary to apply a threshold algorithm which differentiates each one from another ( Thresholding a greyscale image).
At the moment I don't have Matlab with me, but I suggest you have a look at the following documentation about function imbinarize(): https://es.mathworks.com/help/images/ref/imbinarize.html?searchHighlight=threshold#inputarg_method ..specially to the coins example, in which you can see that the medium 'grey' has been supressed from the image.
Regards,

Image Analyst
Image Analyst 2016-11-17
You don't want to use edge detection. Why would you want to do that??? Simply threshold and use sum(), bwarea(), or regionprops()
subplot(2,2,1);
imshow(grayImage, []);
subplot(2,2,2);
histogram(grayImage);
binaryImage = grayImage > someThreshold; %
subplot(2,2,3);
imshow(binaryImage);
area1 = sum(binaryImage(:))
area2 = bwarea(binaryImage)
props = regionprops(bwlabel(binaryImage), 'Area');
area3 = [props.Area]

Andraz Skoda
Andraz Skoda 2016-12-11
Thank you Image Analyst and Bego for advices. But...
The reason I want to use edge detection here is because of the folowing reason:
Since anomalys on my region of interest (ROI-white surface in first image) can accur also very close to edges (as I am showing in the image below) it is imposible to mask that kind of anomalys in a way that you suggested:
So if I use global image thresholding (Otsu) to get mask of ROI on the image above, I get masked image like this:
I don't know how to get this defects on edges. That's the reason I'm experimenting with edge detection. But I also figured out that this edge detection techniques will not work so well.
One of the solutions can be to manualy create mask of the ROI in the image above. But application should run in real time, when also position of surface can be changed a little bit, so predefined manualy created mask will also not work becaouse it will not fit the ROI very precisely...
Any other suggestion how to solve this? This problem is very difficult to solve for me :S I really don't know that else can I try...
Be cool! And thank you all!

类别

Help CenterFile Exchange 中查找有关 Image Processing and Computer Vision 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by