How to find a specific row and column RGB matrix

4 次查看(过去 30 天)
Hello. I want to select a RGB matrix from one image (For example as rows-200 to 220 and coloumn 230to246 matrix) and want to find pixel location of same matrix in another image. For that I wrote below program.Here I1 is first image,I2 is second image and Irefmat is specific matrix from image I1 which I want to find in I2.
Irefmat=I1(100:120,100:120);
red=I2(:,:,1);
green=I2(:,:,2);
blue=I2(:,:,3);
mask=red == Irefmat
green == Irefmat
blue == Irefmat ;
[rows,columns]=find(mask);
subplot(2,2,1);
imshow(mask);
But error is coming. Looking forward for some help!!!
  3 个评论
Constantino Carlos Reyes-Aldasoro
IT would be easier to help if you illustrate your problem with actual images.
Arpit Bhatt
Arpit Bhatt 2021-4-15
i have attached two images.I want to take one pixel matrix from image1 and want to find same matrix in image2 so that I can find how much spatial deviation has happened.Any other methods are also welcomed!!!

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2021-4-16
I do exactly that in my demo of normxcorr2(). I extract out a rectangular ROI (white onion) from an image (that contains the onion) and then find it in the image. I use the same image, but you could just as well find it in a different image.
  1 个评论
Arpit Bhatt
Arpit Bhatt 2021-4-29
编辑:Arpit Bhatt 2021-4-29
Thank you for your reply. Your code workes fine. I am having problem in furthur extension. Actually I am using manual selection of matrix and finding the same matrix in second image. The goal is to find displacement of matrix in terms of rows & columns. The problem is after having xpeak and ypeak, if i compare the results than it gives wrong value. I am sharing two images in which second image is delibrately shifted one row and one column. So the values which the corelation output gives should be nearbz, which is not. Any idea regarding process is welcomed.
smallSubImage = imcrop(rgbImage, [m, n, sampleWidth, sampleHeight]);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(smallSubImage);
subplot(2, 2, 2);
imshow(smallSubImage, []);
set(gcf,'MenuBar','none')
axis on;
caption = sprintf('Sample to Search For, %d rows by %d columns.', rows, columns);
title(caption, 'FontSize', fontSize);
channelToCorrelate = 1; % Use the red channel.
tic
correlationOutput = normxcorr2(smallSubImage(:,:,1), rgbImage(:,:, channelToCorrelate));
toc
subplot(2, 2, 3);
imshow(correlationOutput, []);
axis on;
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(correlationOutput);
caption = sprintf('Normalized Cross Correlation Output, %d rows by %d columns.', rows, columns);
title(caption, 'FontSize', fontSize);
% Find out where the normalized cross correlation image is brightest.
[maxCorrValue, maxIndex] = max(abs(correlationOutput(:)));
[yPeak, xPeak] = ind2sub(size(correlationOutput),maxIndex(1))
% Because cross correlation increases the size of the image,
% we need to shift back to find out where it would be in the original image.
corr_offset = [(xPeak-size(smallSubImage,2)/2) (yPeak-size(smallSubImage,1)/2)]
% Plot it over the original image.
subplot(2, 2, 4); % Re-display image in lower right.
imshow(rgbImage2);
axis on; % Show tick marks giving pixels
hold on; % Don't allow rectangle to blow away image.
% Calculate the rectangle for the template box. Rect = [xLeft, yTop, widthInColumns, heightInRows]
boxRect = [corr_offset(1) corr_offset(2) sampleWidth, sampleHeight]
% Plot the box over the image.
rectangle('position', boxRect, 'edgecolor', 'g', 'linewidth',2);
% Give a caption above the image.
title('Sample area Found in Second Image', 'FontSize', fontSize);
% Display the original color image.
% Plot it over the original image.
subplot(2, 2, 1); % Re-display image in lower right.
imshow(rgbImage);
axis on; % Show tick marks giving pixels
hold on; % Don't allow rectangle to blow away image.
% Calculate the rectangle for the template box. Rect = [xLeft, yTop, widthInColumns, heightInRows]
boxRect = [corr_offset(1) corr_offset(2) sampleWidth, sampleHeight]
% Plot the box over the image.
rectangle('position', boxRect, 'edgecolor', 'g', 'linewidth',2);
% Give a caption above the image.
title('Sample area in First Image', 'FontSize', fontSize);

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by