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

2 views (last 30 days)
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:

Accepted Answer

Wick
Wick on 1 May 2018
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... :)

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!