Filter löschen
Filter löschen

For and While loops

10 Ansichten (letzte 30 Tage)
Zahraa Al-Waeli
Zahraa Al-Waeli am 3 Mai 2021
Bearbeitet: Rena Berman am 16 Jul. 2024 um 17:17
How do I use Embedded “For” or “While” loops to scan through an image in MATLAB?
  1 Kommentar
Chad Greene
Chad Greene am 3 Mai 2021
Can you be more specific about what you're trying to do? What have you tried so far?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Simar
Simar am 12 Okt. 2023
Bearbeitet: Rena Berman am 16 Jul. 2024 um 17:17
Hi Zahraa Al-Waeli,
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

Kategorien

Mehr zu Computer Vision Toolbox Supported Hardware finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by