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

To add intensity values of adjacent pixels that fall between 900 and 1300 in a matrix containing image data, you can define a conditional statement to identify the qualifying pixels, select their adjacent pixels, and add their values.

First, load your image matrix into Matlab using the imread() function. Ensure your matrix is a grayscale image, as the algorithm will not work with colored images.

main.m
matrix = imread('your_image_filename.jpg');
44 chars
2 lines

Next, we can define a nested loop to iterate through each pixel in the matrix to identify qualifying pixels. We'll create an empty matrix called output to store the manipulated image data.

main.m
output = zeros(size(matrix));

for i = 1:size(matrix,1)
    for j = 1:size(matrix,2)
        if matrix(i,j) >= 900 && matrix(i,j) <= 1300
            adjacent_values = [];
            if i > 1 && matrix(i-1,j) >= 900 && matrix(i-1,j) <= 1300
                adjacent_values = [adjacent_values matrix(i-1,j)];
            end
            if i < size(matrix,1) && matrix(i+1,j) >= 900 && matrix(i+1,j) <= 1300
                adjacent_values = [adjacent_values matrix(i+1,j)];
            end
            if j > 1 && matrix(i,j-1) >= 900 && matrix(i,j-1) <= 1300
                adjacent_values = [adjacent_values matrix(i,j-1)];
            end
            if j < size(matrix,2) && matrix(i,j+1) >= 900 && matrix(i,j+1) <= 1300
                adjacent_values = [adjacent_values matrix(i,j+1)];
            end
            output(i,j) = matrix(i,j) + sum(adjacent_values);
        else
            output(i,j) = matrix(i,j);
        end
    end
end
948 chars
25 lines

In the conditional statement, we check if a given pixel has an intensity value between 900 and 1300. If it does, we define an empty matrix called adjacent_values to store the values of any adjacent pixels that also meet the intensity requirements.

We then check each adjacent pixel (above, below, left, right) to see if it meets the criteria, and if it does, we append its value to adjacent_values.

Finally, we add the values of the qualifying pixel and its adjacent pixels (if any) and store the result in the corresponding location in output. If the pixel does not meet the intensity requirements, its value is copied as is to the output matrix.

With these steps completed, your output matrix should now contain your original image data, with the modification of intensity values, where applicable.

gistlibby LogSnag