0

I have an image and a subimage which is cropped out of the original image.

Here's the code I have written so far:

val1 = imread(img);
val2 = imread(img_w);
gray1 = rgb2gray(val1);%grayscaling both images
gray2 = rgb2gray(val2);
matchingval = normxcorr2(gray1,gray2);%normalized cross correlation 
[max_c,imax]=max(abs(matchingval(:)));

After this I am stuck. I have no idea how to change the whole image grayscale except for the sub image which should be in color.

How do I do this?

Thank you.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Jason Thapa
  • 123
  • 9
  • Why are you performing the normalized cross-correlation if all you're doing is replacing a patch in a grayscale image with its colour counterpart? That code is rather unnecessary isn't it? – rayryeng Apr 10 '15 at 02:28

3 Answers3

0

If you know what the coordinates are for your image, you can always just use the rgb2gray on just the section of interest.

For instance, I tried this on an image just now:

im(500:1045,500:1200,1)=rgb2gray(im(500:1045,500:1200,1:3)); im(500:1045,500:1200,2)=rgb2gray(im(500:1045,500:1200,1:3)); im(500:1045,500:1200,3)=rgb2gray(im(500:1045,500:1200,1:3)); Where I took the rows (500 to 1045), columns (500 to 1200), and the rgb depth (1 to 3) of the image and applied the rgb2gray function to just that. I did it three times as the output of rgb2gray is a 2d matrix and a color image is a 3d matrix, so I needed to change it layer by layer.

This worked for me, making only part of the image gray but leaving the rest in color.

The issue you might have though is this, a color image is 3 dimensions while a gray scale need only be 2 dimensions. Combining them means that the gray scale must be in a 3d matrix.

Depending on what you want to do, this technique may or may not help.

Mayuri
  • 11
  • 2
  • You are solving the reverse problem. You are converting a section of the image to be grayscale. The OP wants to convert the entire image to grayscale **except** for a subimage. Also, calling `rgb2gray` three times is unnecessary. Simply call it once, then use `cat` to duplicate the grayscale subimage to be placed in the colour image: `gray = rgb2gray(im(500:1045,500:1200,:)); im(500:1045,500:1200,:) = cat(3, gray, gray, gray);`. – rayryeng Apr 10 '15 at 02:55
0

Judging from your code, you are reading the image and the subimage in MATLAB. What you need to know are the coordinates of where you extracted the subimage. Once you do that, simply take your original colour image, convert that to grayscale, then duplicate this image in the third dimension three times. You need to do this so that you can place colour pixels in this image.

For RGB images, grayscale images have the RGB components to all be the same. Duplicating this image in the third dimension three times creates the RGB version of the grayscale image. Once you do that, simply use the row and column coordinates of where you extracted the subimage and place that into the equivalent RGB grayscale image.

As such, given your colour image that is stored in img and your subimage stored in imgsub, and specifying the rows and columns of where you extracted the subimage in row1,col1 and row2,col2 - with row1,col1 being the top left corner of the subimage and row2,col2 is the bottom right corner, do this:

img_gray = rgb2gray(img);
img_gray = cat(3, img_gray, img_gray, img_gray);
img_gray(row1:row2, col1:col2,:) = imgsub;

To demonstrate this, let's try this with an image in MATLAB. We'll use the onion.png image that's part of the image processing toolbox in MATLAB. Therefore:

img = imread('onion.png');

Let's also define our row1,col1,row2,col2:

row1 = 50;
row2 = 90;
col1 = 80;
col2 = 150;

Let's get the subimage:

imgsub = img(row1:row2,col1:col2,:);

Running the above code, this is the image we get:

enter image description here

rayryeng
  • 102,964
  • 22
  • 184
  • 193
0

I took the same example as rayryeng's answer and tried to solve by HSV conversion. The basic idea is to set the second layer i.e saturation layer to 0 (so that they are grayscale). then rewrite the layer with the original saturation layer only for the sub image area, so that, they alone have the saturation values.

Code:

img = imread('onion.png');
img = rgb2hsv(img);
sPlane = zeros(size(img(:,:,1)));
sPlane(50:90,80:150) = img(50:90,80:150,2);
img(:,:,2) = sPlane;
img = hsv2rgb(img);
imshow(img);

Output: (Same as rayryeng's output)

enter image description here

Related Answer with more details here

Community
  • 1
  • 1
Santhan Salai
  • 3,888
  • 19
  • 29