add to image matrix by including only adjacent pixels with intensity values between 900 and 1300 in matlab

main.m
% read the image into a matrix
img = imread('example.jpg');

% create a new matrix to hold the updated values
new_img = double(img);

% loop through each pixel in the image (except for the edges)
for i = 2:size(img,1)-1
    for j = 2:size(img,2)-1
        % get the intensity value of the current pixel
        intensity = img(i,j);
        
        % check if the intensity value is between 900 and 1300
        if intensity >= 900 && intensity <= 1300
            % check if the intensity values of adjacent pixels are also between 900 and 1300
            if img(i-1,j) >= 900 && img(i-1,j) <= 1300 && ...
               img(i+1,j) >= 900 && img(i+1,j) <= 1300 && ...
               img(i,j-1) >= 900 && img(i,j-1) <= 1300 && ...
               img(i,j+1) >= 900 && img(i,j+1) <= 1300
                   % average the intensity values of the current pixel and its adjacent pixels
                   new_img(i,j) = (img(i-1,j) + img(i+1,j) + img(i,j-1) + img(i,j+1) + intensity) / 5;
            end
        end
    end
end

% convert the matrix back to an image and display it
new_img = uint8(new_img);
imshow(new_img);
1123 chars
30 lines

This code reads in an image matrix and creates a new matrix of the same size to hold the updated values. It then loops through each pixel (except for the edges) of the original matrix and checks if the intensity value is between 900 and 1300. If it is, it checks if the intensity values of the adjacent pixels are also between 900 and 1300 using logical indexing. If they are, it calculates the average of the intensity values of the current pixel and its adjacent pixels and stores the result in the corresponding pixel of the new matrix. The new matrix is then converted back to an image and displayed.

gistlibby LogSnag