Threshold detection for specific region
10 次查看(过去 30 天)
显示 更早的评论
Hello to all MATLAB lad!!
I'm doing the master degree project that related to detecting the threshold value on specific cropped region of interest on the image. For my pre-project test work, I'm creating a blank square with 4 subset circles on its corners. The color of square and circle is clearly different so that its yield different threshold value. The objective of this code is to recognized the threshold value at the circle region on the corner of the square and plot a rectangular mask over the circle region .
This is how far I have done. However, no rectangular is being plotted :(
This is my code:
Image = imread('fourCIR.jpg');
subplot(2, 3, 1);
imshow(Image);
title('Original Image', 'FontSize', 10);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo by StalkerZ+','numbertitle','off')
grayImage = rgb2gray(Image);
[rows columns numberOfColorBands] = size(grayImage);
subplot(2, 3, 2);
imshow(grayImage,[]);
title('Grayscale Image', 'FontSize', 10);
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 3, 3);
bar(pixelCount);
grid on;
title('Histogram of Grayscale image', 'FontSize', 10);
threshold.level = 0.5;
binaryImage = im2bw(grayImage,threshold.level);
subplot(2, 3, 4);
imshow(binaryImage, []);
title('Original Binary Image', 'FontSize', 10);
level = threshold.level.*255;
Area1 = imcrop(grayImage,[9.5 9.5 317 208]);
Area2 = imcrop(grayImage,[822.5 11.5 317 208]);
Area3 = imcrop(grayImage,[8.5 404.5 317 208]);
Area4 = imcrop(grayImage,[828.5 408.5 317 208]);
subplot(2, 3, 5);
imshow(binaryImage, []);
for i=1:rows
for j=1:columns
switch level
case Area1 > (level-5);
yellow = uint8([255 255 0]);
rectangle = uint8([9.5 9.5 317 208]);
J = step(shapeInserter, b, rectangle,'BorderColor','Custom',...
'CustomBorderColor',yellow);
figure
plot(J)
case Area2 > (level-5)
yellow = uint8([255 255 0]);
rectangle = uint8([822.5 11.5 317 208]);
J = step(shapeInserter, b, rectangle,'BorderColor','Custom',...
'CustomBorderColor',yellow);
figure
plot(J);
case Area3 > (level-5)
yellow = uint8([255 255 0]);
rectangle = uint8([8.5 404.5 317 208]);
J = step(shapeInserter, b, rectangle,'BorderColor','Custom',...
'CustomBorderColor',yellow);
figure
plot(J);
case Area4 > (level-5)
yellow = uint8([255 255 0]);
rectangle = uint8([828.5 408.5 317 208]);
J = step(shapeInserter, b, rectangle,'BorderColor','Custom',...
'CustomBorderColor',yellow);
figure
plot(J);
otherwise
end
end
end
title('Threshold Detection', 'FontSize', 10);
Please kindly advice me on this one. Sorry if my explanation is unclear.
0 个评论
回答(2 个)
Cyrus
2016-7-15
编辑:Cyrus
2016-7-15
So you could generate the Binary Image, why do you still need to find the threshold? is finding the threshold something you really need to?
If your goal is just to mask them, there are other ways you can do this:
you can use
stats = regionprops(BW,properties) on the Binary image.
Circels = regionprops(binaryImage, 'Area', 'Centroid', 'PixelList');
In case there are some noises in the image you can use 'Area' option to remove them, then you can use either Centroid to create a rectangle and cover the circles, or using PixelList and a nested loop to change the color of them in the original image.
There are also some other properties in the "regionprops" function you might be interested to use.
0 个评论
Image Analyst
2016-7-15
I hope there's more to your masters than this.
You can use plot() or rectangle() to place a box in the overlay above the image provided you know the location. See a related thing where I find round coins and get the bounding box then use that to crop out the coins:
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Computer Vision with Simulink 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!