Why does Conv2 change the colormap of the base image?

5 次查看(过去 30 天)
Hi, I have some images that I display using a matlab app (as an image, rather than uiaxes). I've created a function that overlays an arrow to indicate direction. This was done using a simple for loop, considering the dimensions of the image.
The image is first loaded using dicomread, then converted to rgb using a community funtion (grs2rgb) so that its compatible with the image properties of the app (i think app.image uses imshow). After this, I use my function to add the arrow to the image data. This all works fine, and I can display my grayscale image with a single-pixel-wide red arrow incorporated into the image data.
I thought the arrow was a bit too skinny, so I've fattened the line in the for loop with an extra + and - 1, but I couldn't find an elegant way to do this for the tip of the arrow, although I'm sure there is. Instead, I use conv2 to convolve a [0 1 0;1 1 1;0 1 0] kernel with the tip of the arrow, before adding the stem of the arrow, all within the function. Crucially, incorporating this convolution somehow changes the image such that the base image is no longer grayscale, but all resembles the summer colormap or similar (tinged red). Why is this? Please see my code below, the offending portion of code is in bold.
The below code produces the odd color scheme:
offset = round(0.12*size(image,2));
s1 = round(0.1*size(image,1));
s2 = round(0.9*size(image,1));
for i = 1:7
image(s1+i,offset+i) = 1;
image(s1+i,offset-i) = 1;
image(s2-i,offset+i) = 1;
image(s2-i,offset-i) = 1;
end
convmat = [0 1 0;1 1 1;0 1 0];
mask = conv2(image(:,:,1),convmat,'same');
image(:,:,1) = image(:,:,1) + mask;
image(s1:s2,offset-1:offset+1) = 1; % I don't believe declaring the third dimension does anything here, as the ones are just written to the top (red) layer of the matrix.
The below code produces preserves the grayscale image and overlays a skinny red arrow:
offset = round(0.12*size(image,1));
s1 = round(0.1*size(image,2));
s2 = round(0.9*size(image,2));
image(offset,s1:s2) = 1;
for i = 1:7
image(offset+i,s1+i) = 1;
image(offset-i,s1+i) = 1;
image(offset+i,s2-i) = 1;
image(offset-i,s2-i) = 1;
end
  1 个评论
Cameron Starling
Cameron Starling 2022-2-18
I have figured out the elegant(ish) solution... just adding extra lines within the for loop
image(offset-i,s2-i-1) = 1; and image(offset-i,s2-i-2) = 1;
But the original question still stands, what is conv2 doing to the image data?

请先登录,再进行评论。

回答(1 个)

yanqi liu
yanqi liu 2022-2-21
yes,sir,may be use im2uint8(mat2gray(your_conv2_outptut)) to display

类别

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

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by