Imshow and Imshowpair differences?

Illustration
Michael Vanegas - 2023-02-08T13:47:45+00:00
Question: Imshow and Imshowpair differences?

As a way to keep busy I have been teaching myself how to perform edge detection and code them up myself. When attemtpting to hardcode my own Canny detector, I have run into a problem where imshowpair and imshow display different intensity values across the same pixel of the same displayed image input. I have messed with the scaling operator and set it to 'none' but the issue still persists? Any tips on why this is the case?   I have attached my script below, be wary it is a bit long and convuluted but the final step should be where the imshow and imshowpair issue come in.     q = imread('circuit.tif'); %% STEP1: Apply Gaussian Filter q_output1 = imgaussfilt(q,1.5); %JUST FOR FUN, I AM PERFORMING BOTH PREWITT AND SOBEL TO PERFORM THE CANNY Canny_Prewitt_KernalX = [-1 0 1;-1 0 1;-1 0 1]; Canny_Prewitt_KernalY = Canny_Prewitt_KernalX'; Canny_Sobel_KernalX = [-1 0 1;-2 0 2;-1 0 1]; Canny_Sobel_KernalY = -Canny_Sobel_KernalX'; % STEP2: GRADIENT CALCULATION Canny_Prewitt_FiltX = filter2(Canny_Prewitt_KernalX,q_output1,'same'); % Gx Canny_Prewitt_FiltY = filter2(Canny_Prewitt_KernalY,q_output1,'same'); % Gy Canny_Prewitt_Edge = sqrt(Canny_Prewitt_FiltX.^2 + Canny_Prewitt_FiltY.^2); % V - Gradient Intensity Matrix Canny_Sobel_FiltX = filter2(Canny_Sobel_KernalX,q_output1,'same'); % Gx Canny_Sobel_FiltY = filter2(Canny_Sobel_KernalY,q_output1,'same'); % Gy Canny_Sobel_Edge = sqrt(Canny_Sobel_FiltX.^2 + Canny_Sobel_FiltY.^2); % V - Gradient Intensity Matrix Canny_Prewitt_Theta = atan2(Canny_Prewitt_FiltY,Canny_Prewitt_FiltX).*(180/pi); % Theta Canny_Sobel_Theta = atan2(Canny_Sobel_FiltY,Canny_Sobel_FiltX).*(180/pi); % Theta % The theta areas contain NaN values so lets replace them with 0. Canny_Prewitt_Theta(isnan(Canny_Prewitt_Theta)) = 0; Canny_Sobel_Theta(isnan(Canny_Sobel_Theta)) = 0; % To correct for same angles in the negative just add 360 to any value less than 0 rows = size(Canny_Prewitt_Theta,1); columns = size(Canny_Sobel_Theta,2); for i = 1:rows % THIS FOR LOOP TURNS -ANGLES TO A RANGE OF 0-180! for j = 1:columns if Canny_Prewitt_Theta(i,j) < 0 Canny_Prewitt_Theta(i,j) = Canny_Prewitt_Theta(i,j) + 180; elseif Canny_Sobel_Theta(i,j) < 0 Canny_Sobel_Theta(i,j) = Canny_Sobel_Theta(i,j) +180; elseif Canny_Prewitt_Theta(i,j) < 0 && Canny_Sobel_Theta(i,j) < 0 Canny_Prewitt_Theta(i,j) = Canny_Prewitt_Theta(i,j) + 180; Canny_Sobel_Theta(i,j) = Canny_Sobel_Theta(i,j) +180; end end end Canny_BW_Prewitt_Edge = imbinarize(Canny_Prewitt_Edge/255,0.3); Canny_BW_Sobel_Edge = imbinarize(Canny_Sobel_Edge/255,0.3); % STEP3: NON-MAXIMUM SUPRESSION rows = size(Canny_Prewitt_Theta,1); columns = size(Canny_Sobel_Theta,2); % THESE FOR LOOPS FORM THE LOGIC FOR THE IMAGE NON-MAX SUPRESSION TEST for i = 2:rows-1 for j = 2:columns-1 Prewitt_Result = [1 2 3 4 5].*... [Canny_Prewitt_Theta(i,j) < 22.5,... Canny_Prewitt_Theta(i,j) >= 22.5 && Canny_Prewitt_Theta(i,j) < 67.5,... Canny_Prewitt_Theta(i,j) >= 67.5 && Canny_Prewitt_Theta(i,j) < 112.5,... Canny_Prewitt_Theta(i,j) >= 112.5 && Canny_Prewitt_Theta(i,j) < 157.5,... Canny_Prewitt_Theta(i,j) >= 157.5 && Canny_Prewitt_Theta(i,j) <= 180]; switch Prewitt_Result(Prewitt_Result~=0) case 1 if Canny_Prewitt_Edge(i,j-1) > Canny_Prewitt_Edge(i,j) Canny_Prewitt_Edge(i,j) = 0; elseif Canny_Prewitt_Edge(i,j+1) > Canny_Prewitt_Edge(i,j) Canny_Prewitt_Edge(i,j) = 0; end case 2 if Canny_Prewitt_Edge(i-1,j+1) > Canny_Prewitt_Edge(i,j) Canny_Prewitt_Edge(i,j) = 0; elseif Canny_Prewitt_Edge(i+1,j-1) > Canny_Prewitt_Edge(i,j) Canny_Prewitt_Edge(i,j) = 0; end case 3 if Canny_Prewitt_Edge(i-1,j) > Canny_Prewitt_Edge(i,j) Canny_Prewitt_Edge(i,j) = 0; elseif Canny_Prewitt_Edge(i+1,j) > Canny_Prewitt_Edge(i,j) Canny_Prewitt_Edge(i,j) = 0; end case 4 if Canny_Prewitt_Edge(i-1,j-1) > Canny_Prewitt_Edge(i,j) Canny_Prewitt_Edge(i,j) = 0; elseif Canny_Prewitt_Edge(i+1,j+1) > Canny_Prewitt_Edge(i,j) Canny_Prewitt_Edge(i,j) = 0; end case 5 if Canny_Prewitt_Edge(i,j-1) > Canny_Prewitt_Edge(i,j) Canny_Prewitt_Edge(i,j) = 0; elseif Canny_Prewitt_Edge(i,j+1) > Canny_Prewitt_Edge(i,j) Canny_Prewitt_Edge(i,j) = 0; end end Sobel_Result = [1 2 3 4 5].*... [Canny_Sobel_Theta(i,j) < 22.5,... Canny_Sobel_Theta(i,j) >= 22.5 && Canny_Sobel_Theta(i,j) < 67.5,... Canny_Sobel_Theta(i,j) >= 67.5 && Canny_Sobel_Theta(i,j) < 112.5,... Canny_Sobel_Theta(i,j) >= 112.5 && Canny_Sobel_Theta(i,j) < 157.5,... Canny_Sobel_Theta(i,j) >= 157.5 && Canny_Sobel_Theta(i,j) <= 180]; switch Sobel_Result(Sobel_Result~=0) case 1 if Canny_Sobel_Edge(i,j-1) > Canny_Sobel_Edge(i,j) Canny_Sobel_Edge(i,j) = 0; elseif Canny_Sobel_Edge(i,j+1) > Canny_Sobel_Edge(i,j) Canny_Sobel_Edge(i,j) = 0; end case 2 if Canny_Sobel_Edge(i-1,j+1) > Canny_Sobel_Edge(i,j) Canny_Sobel_Edge(i,j) = 0; elseif Canny_Sobel_Edge(i+1,j-1) > Canny_Sobel_Edge(i,j) Canny_Sobel_Edge(i,j) = 0; end case 3 if Canny_Sobel_Edge(i-1,j) > Canny_Sobel_Edge(i,j) Canny_Sobel_Edge(i,j) = 0; elseif Canny_Sobel_Edge(i+1,j) > Canny_Sobel_Edge(i,j) Canny_Sobel_Edge(i,j) = 0; end case 4 if Canny_Sobel_Edge(i-1,j-1) > Canny_Sobel_Edge(i,j) Canny_Sobel_Edge(i,j) = 0; elseif Canny_Sobel_Edge(i+1,j+1) > Canny_Sobel_Edge(i,j) Canny_Sobel_Edge(i,j) = 0; end case 5 if Canny_Sobel_Edge(i,j-1) > Canny_Sobel_Edge(i,j) Canny_Sobel_Edge(i,j) = 0; elseif Canny_Sobel_Edge(i,j+1) > Canny_Sobel_Edge(i,j) Canny_Sobel_Edge(i,j) = 0; end end end end % Image borders cannot be traversed by the FOR Loop, so set to 0! % WARNING!! THIS CAUSES SOME ISSUES CHECK AGAIN AFTER FINISHING Canny_Prewitt_Edge(1,:) = 0; Canny_Prewitt_Edge(max(rows),:) = 0; Canny_Prewitt_Edge(:,1) = 0; Canny_Prewitt_Edge(:,max(columns)) = 0; Canny_Sobel_Edge(1,:) = 0; Canny_Sobel_Edge(max(rows),:) = 0; Canny_Sobel_Edge(:,1) = 0; Canny_Sobel_Edge(:,max(columns)) = 0; figure(34) imshowpair(Canny_Prewitt_Edge,Canny_Sobel_Edge,'montage','Scaling','none');title('Non-Max Suppression'); figure(35) subplot(121),imshow(Canny_Prewitt_Edge,colormap('gray'));shtitle('Non-Max Suppression');title('Canny using Prewitt'); subplot(122),imshow(Canny_Sobel_Edge,colormap('gray'));title('Canny using Sobel');  

Expert Answer

Profile picture of Kshitij Singh Kshitij Singh answered . 2025-11-20

You can tell imshow() what values to map to zero and what to map to 255 via the input arguments. 

 

imshow(grayImage, [40, 200]); % 40 will be black (0) and 200 will be white (255)

Plus you can stitch together images like

wideImage = [image1, image2];

which is essentially what showpair() does. Then you can use imshow() like above.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!