簡體   English   中英

如何使用Matlab檢測局部矩形並對齊圖像?

[英]How can I detect a partial rectangle and align image using Matlab?

我想知道如何才能檢測到我手動制作的紙正方形的右下角(可能不是完美的正方形,因為我自己畫了它)在另一個對象之上。 我想要做的是使用這個正方形作為參考,並根據正方形的右下角對齊圖像。 我是Matlab的業余愛好者。 有人可以幫忙嗎?

左上方的正方形是我需要檢測的紙張正方形,它是參考點。

左上角帶有正方形的示例圖像

您有很多選擇來檢測正方形。 它實際上是一個矩形; 您想要檢測,但是最簡單的方法是使用形態學運算。 在這里,我提供了一個注釋腳本來幫助您; 因為您是MATLAB的業余愛好者。

ShowMaskAsOverlay不是MATLAB中的內置命令,它非常有用。 如果要下載,請訪問此鏈接

根據您對問題的了解,我會提出另一種建議。 您可以在制作正方形之前保存圖像,在制作正方形之后保存其他圖像。 然后,您只需要獲取兩個圖像之間的差異即可。

close all;clear all;clc;

% Read in Image
im = imread('stack.png');

[r, c, ch] = size(im);

% Convert Image to Gray if it's RGB
if ch == 3
    gray = rgb2gray(im);
end

% Threshold Value; > 0.87 = 1 and  < 0.87 = 0
thresh = 0.87;
bw = im2bw(gray, thresh);

% Removing unwanted white pixels
bw = bwareaopen(bw, 400);
bw = imerode(bw, strel('disk', 3));
bw = imfill(bw, 'holes');
bw = bwareaopen(bw, 600);
bw = imdilate(bw, strel('disk', 3)); %optional. Could be omitted

% Calculating the BoundingBox of the Square
s = regionprops(bw, 'BoundingBox');
box = struct2cell(s);
pos = cell2mat(box);

%% Displaying Images
figure(1)
imshow(im)
rectangle('position', pos, 'edgecolor', 'red', 'linewidth', 1.5)
str = ['Bounding Box Bottom Right Corner Coordinate Value = ', num2str(pos(4))];
h = text(pos(3), pos(4), num2str(pos(4)));
set(h, 'Color', 'k', 'FontSize', 12, 'FontWeight', 'bold')
title(str)

figure(2)
imshow(gray)
showMaskAsOverlay(0.5, bw, 'g')

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM