Threshold detection for specific region

11 次查看(过去 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.

回答(2 个)

Cyrus
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.

Image Analyst
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:
  1 个评论
Issara Laosuwan
Issara Laosuwan 2016-7-20
Hello! Sorry for late reply. I study your code and try apply it for my work, this time with real-time webcam. Still not working :{
vid = videoinput('winvideo', 1,'YUY2_640x480');
vid.FramesPerTrigger = 1;
vid.ReturnedColorspace = 'rgb';
triggerconfig(vid, 'manual');
vidRes = get(vid, 'VideoResolution');
imWidth = vidRes(1);
imHeight = vidRes(2);
nBands = get(vid, 'NumberOfBands');
while (true)
Image = getsnapshot(vid);
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);
xlim([0 grayLevels(end)]);
grid on;
title('Histogram of Grayscale image', 'FontSize', 10);
thresholdValue = 175;
NormalizedThresholdValue = thresholdValue/255;
filterImage = medfilt2(grayImage,[3,3]);
binaryImage = im2bw(filterImage,NormalizedThresholdValue);
binaryImage = imfill(binaryImage, 'holes');
hold on;
maxYValue = ylim;
line([thresholdValue, thresholdValue], maxYValue, 'Color', 'r');
subplot(2, 3, 4);
SE = strel('disk',15);
BW = imdilate(binaryImage,SE);
BW2 = bwareaopen(BW, 300);
imshow(BW2);
title('Binary Image, obtained by thresholding', 'FontSize', 10);
Area1 = imcrop(filterImage, [138 34.9999999999999 93 70]);
Area2 = imcrop(filterImage, [170 138 82 76]);
Area3 = imcrop(filterImage, [174 255 106 90]);
subplot(2, 3, 5);
imshow(Image);
title('Object Detection', 'FontSize', 10);
hold on
boundaries1 = bwboundaries(Area1);
boundaries2 = bwboundaries(Area2);
boundaries3 = bwboundaries(Area3);
numberOfBoundaries1 = size(boundaries1, 1);
numberOfBoundaries2 = size(boundaries2, 1);
numberOfBoundaries3 = size(boundaries3, 1);
for k = 1 : numberOfBoundaries1
thisBoundary = boundaries1{k};
if Area1 < thresholdValue
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
hold off
else
end
end
for k = 1 : numberOfBoundaries2
thisBoundary = boundaries2{k};
if Area2 < thresholdValue
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
hold off
else
end
end
for k = 1 : numberOfBoundaries3
thisBoundary = boundaries3{k};
if Area3 < thresholdValue
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
hold off
else
end
end
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Computer Vision with Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by