[英]How can I detect a rectangle with a certain dimensions in this picture, and compare the rectangle dimensions with predetermined dimensions in Matlab?
您可以对图像进行一些预处理(特定通道的阈值,此处为绿色通道)和中值滤波器,以去除不需要的背景信号。
然后是使用regionprops识别图像中的对象的问题。 电池是面积最大的对象,因此您可以使用regionprops
返回的结构中的适当索引来获取封闭边界框的大小。
clear
clc
%/ Read and pre-process the image to clear unwanted signal
Im = imread('Battery.jpg');
ImBW = im2bw(Im(:,:,2),.25);
ImBW = medfilt2(ImBW,[7 7]);
%// Detect objects in cleaned image
S = regionprops(ImBW,'BoundingBox','Area');
%// Identify battery as largest object
[MaxArea,MaxIndex] = max(vertcat(S.Area));
imshow(Im,'InitialMagnification',20)
%// Display results and message
hold on
rectangle('Position',S(MaxIndex).BoundingBox,'LineWidth',2,'EdgeColor','y')
Length = S(MaxIndex).BoundingBox(3);
Height = S(MaxIndex).BoundingBox(4);
message = sprintf('The height is %0.2f pixels \nand length is %0.2f pixels',Height,Length);
h = msgbox(message);
裁剪后的输出:
然后,您只需要将像素值转换为真实单位即可。 我会让那部分交给你。
玩得开心!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.