Hello! I have a homework about pattern search using segmentation, and I got this error message: Array dimensions must match for binary array op. I only have the searching part of the code (or I hope this code do that).

1 回表示 (過去 30 日間)
img = imread('brickwall.jpg');
part = imread('brickwall_sample2.png');
[rows, cols] = size(img);
[part_row, part_col] = size(part);
diffimg = zeros(size(img));
minx = 1; miny = 1; minval = Inf;
for x=1:rows - part_row
for y=1:cols - part_col
cut = img(x:x+part_row-1, y:y+part_col-1);
diff = sum(sum( abs(cut - part) )); %this is where the problem occurs
diffimg(x,y) = diff;
if ( diff < minval )
minval = diff;
minx = x;
miny = y;
end
end
end
imshow(diffimg,[]);
result = zeros([rows, cols, 3],'uint8');
result(:,:,1) = img;
result(:,:,2) = img;
result(:,:,3) = img;
result(minx, miny:miny+part_col-1,1) = 255;
result(minx+part_row-1, miny:miny+part_col-1,1) = 255;
result(minx:minx+part_row-1, miny,1) = 255;
result(minx:minx+part_row-1, miny+part_col-1,1) = 255;
imshow(result,[]);
Thank you for the answers :)
And the two pictures are here:

採用された回答

Wick
Wick 2018 年 5 月 1 日
This isn't an answer to fix your code. This is an answer as to why it breaks. Try inserting these two lines:
size(cut)
size(part)
after you define cut and before you define diff. cut is [155 x 1311] where as part is [155 x 437 x 3]
The imread command naturally returns a three-page variable. Each n x m matrix represents the red, green, and blue channel. When you defined cut you only defined it on the first page. But you're comparing it to the three-paged part variable. Since it's homework, I'll leave the rest to you... :)
  1 件のコメント
Dorottya Forrai
Dorottya Forrai 2018 年 5 月 3 日
Thank you for your answer :) It helps a lot, and I found the brick in the picture :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by