For and While loops

1 次查看(过去 30 天)
Zahraa Al-Waeli
Zahraa Al-Waeli 2021-5-3
评论: Umar 2024-7-30
How do I use Embedded “For” or “While” loops to scan through an image in MATLAB?
  2 个评论
Chad Greene
Chad Greene 2021-5-3
Can you be more specific about what you're trying to do? What have you tried so far?
Umar
Umar 2024-7-30

Hi @Zahraa Al-Waeli,

To illustrate it by example code snippet, I will first load an example image using the imread function, then extract the number of rows and columns in the image using the size function. Next, use nested "for" loops to iterate through each pixel of the image. The outer loop (for i = 1:rows) iterates over the rows, while the inner loop (for j = 1:cols) iterates over the columns. Within the nested loops, I will access the pixel value at position (i, j) in the image matrix using img(i, j).

You can perform various operations on the pixel values inside the loop, such as displaying the pixel values, modifying them, or analyzing them. Here is example code snippet,

% Read and display the image

img = imread('/MATLAB Drive/IMG_7236.jpg');

% Get the size of the image (rows and columns)

[rows, cols, ~] = size(img);

% Nested for loops to scan through each pixel

for i = 1:rows

    for j = 1:cols
        % Access pixel values at position (i, j)
        pixel_value = img(i, j);
        % Perform operations on the pixel value (e.g., display, modify, analyze)
        disp(['Pixel value at position (', num2str(i), ',', num2str(j), '): ', num2str(pixel_value)]);
    end

end

Please see attached results.

Please let us know if you have any further questions.

请先登录,再进行评论。

回答(1 个)

Simar
Simar 2023-10-12
编辑:Simar 2024-7-30
Hi Zahraa,
I understand that you are looking to use embedded ‘for’ or ‘while’ loops to scan through an image in MATLAB. Scanning through an image in MATLAB using embedded for or while loops is a common task in image processing. Here is a simple example of how you can do it:
1. Firstly, read the image file into MATLAB using ‘imread’ function and store the image data in a variable named ‘img’.
img = imread('your_image_file.jpg');
2. For this example, using the ‘if statement to check if image is a colored image (which has three layers - red, green, and blue). If it is, the rgb2gray function converts the color image into a grayscale image (black and white) for simplicity.
if size(img, 3) == 3
img = rgb2gray(img);
end
3. Getting size of the image in terms of number of rows and columns. Think of the image as a grid, where each cell in the grid is a pixel (the smallest unit of an image). The number of rows and columns tells us the height and width of this grid.
[rows, cols] = size(img);
4. In this example, using two ‘for’ loops (one inside the other) to scan through the image. The external loop iterates over the rows of the image and the internal loop iterates over the columns. Hence including every cell (or pixel) in the grid. For each pixel, retrieve its value, perform an operation (in this case, inversion), and then save the result back to the image.
for i = 1:rows
for j = 1:cols
% Getting value of the pixel at row i and column j. In a grayscale image, this value is a number between 0 (black) and 255 (white)
pixel = img(i, j);
% Perform operations on the pixel - Subtracting the pixel value from 255 (the maximum possible value), effectively inverting the image (making dark areas light, and light areas dark).
img(i, j) = 255 - pixel;
end
end
5. Finally, displaying the modified image using ‘imshow’ function.
imshow(img);
Here is the complete code:
% Load an image
img = imread('your_image_file.jpg ');
% Convert to grayscale if the image is RGB
if size(img, 3) == 3
img = rgb2gray(img);
end
% Get the size of the image
[rows, cols] = size(img);
% Scan through the image
for i = 1:rows
for j = 1:cols
% Get the pixel value
pixel = img(i, j);
% Perform operations on the pixel here
% For example, let us invert the image
img(i, j) = 255 - pixel;
end
end
% Display the image
imshow(img);
The code loads an image, goes through each pixel one by one, inverts the color of the pixel, and then displays the modified image.
Remember that MATLAB is a high-level language, and it is more efficient to use matrix operations when possible, instead of loops. However, in some cases, loops are necessary and the above example shows how to use them.
Refer to the documentation links below to know more about ‘for’ and ‘while’ loops:
Hope it helps!
Best Regards
Simar

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by