[英]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.