MATLAB: I need code urgently for the project, please help me

comparisondiagonal elementsImage Processing Toolbox

i want code for comparison of pixels in an image by considering a 3*3 matrix. for each pixel, a 3*3 matrix around the pixel should be considered and the pixel must be compared with both the diagonals of the considered 3*3 matrix, whether the pixel is greater than all the diagonal elements or not?

Best Answer

If you're not turning it in for homework, you can use my code below which uses highly optimized 2-D convolution:
grayImage = imread('cameraman.tif');
subplot(1, 2, 1);
imshow(grayImage, []);
% Do one corner
kernel = [-1,0,0;0,1,0; 0,0,0];
filter1 = conv2(grayImage, kernel, 'same');
% Do another corner


kernel = [0,0,-1;0,1,0; 0,0,0];
filter2 = conv2(grayImage, kernel, 'same');
% Do another corner
kernel = [0,0,0;0,1,0; -1,0,0];
filter3 = conv2(grayImage, kernel, 'same');
% Do another corner
kernel = [0,0,0;0,1,0; 0,0,-1];
filter4 = conv2(grayImage, kernel, 'same');
% Output must have middle pixel greater than all 4 corners.
outputImage = (filter1 > 0) & (filter2 > 0) & (filter3 > 0) & (filter4 > 0);
subplot(1, 2, 2);
imshow(outputImage, []);
0000 Screenshot.png
Related Question