Michael Vanegas asked . 2023-02-08

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');

 

image processing , edge detection , Image Processing and Computer Vision

Expert Answer

Kshitij Singh answered . 2024-12-21 16:41:02

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

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.