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... :)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by