Color Imaging - RGB channel

10 Ansichten (letzte 30 Tage)
Sachin Nath P M
Sachin Nath P M am 2 Jul. 2020
Bearbeitet: DGM am 1 Jan. 2024
There is a image which contains 3 color channels RGB and we should crop it and overlay them one on another to get a color image. The image is :-
I have written a code for this problem :-
img = imread('image.jpg');
[r,c] = size(img);
rr=r/3;
B=imcrop(img,[1,1,c,rr]);
G=imcrop(img,[1,1*rr,c,rr]);
R=imcrop(img,[1,2*rr,c,rr]);
ColorImg(:,:,1) = R;
ColorImg(:,:,2) = G;
ColorImg(:,:,3) = B;
imshow(ColorImg)
but I am getting an error as : -
Variable B must be of size [341 400]. It is currently of size [342 400]. Check where the variable is assigned a value.
Can you help me to know what is the error?
Thank you in advance.

Akzeptierte Antwort

Image Analyst
Image Analyst am 2 Jul. 2020
It worked for me.
though you're forgetting to add 1 so they're misaligned vertically. Also you should never use the size function like that on unknown images without asking for the number of color channels or else if it's color, your columns will be three times too big. Also, imcrop() is a weird function. By default it wants xLeft and yTop values that are half a pixel smaller than the actual row and column you want to crop on. And the width is from pixel center to pixel center, so you'd want to subtract 1 from the number of rows and columns.
Try this corrected code:
img = imread('image.jpeg');
subplot(1, 2, 1);
imshow(img);
[rows, columns, numberOfColorChannels] = size(img)
rowsForOneImage = rows/3
fprintf('Cropping B from row %d to %d.\n', 1, rowsForOneImage);
B=imcrop(img,[1, 1, columns - 1, rowsForOneImage - 1]);
fprintf('Cropping G from row %d to %d.\n', 1*rowsForOneImage + 1, 1*rowsForOneImage + rowsForOneImage);
G=imcrop(img,[1, 1*rowsForOneImage + 1, columns - 1, rowsForOneImage - 1]);
fprintf('Cropping R from row %d to %d.\n', 2*rowsForOneImage + 1, 2*rowsForOneImage + rowsForOneImage);
R=imcrop(img,[1, 2*rowsForOneImage + 1, columns - 1, rowsForOneImage - 1]);
ColorImg(:,:,1) = R;
ColorImg(:,:,2) = G;
ColorImg(:,:,3) = B;
subplot(1, 2, 2);
imshow(ColorImg)
  4 Kommentare
Image Analyst
Image Analyst am 1 Apr. 2021
@Raja Bose, it works. I just downloaded the image and tried my code again. There is no error. You must have changed something, like the image.
Avinash
Avinash am 14 Feb. 2023
Thank you for the explnation.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 2 Jul. 2020
Here's another way to do it using indexing instead of imcrop():
img = imread('image.jpeg');
subplot(1, 2, 1);
imshow(img);
[rows, columns, numberOfColorChannels] = size(img)
fprintf('Rows = %d, which means %f rows per image.\n', rows, rows/3);
row1 = [1, rows/3+1, 2*rows/3+1]
row2 = row1 + rows/3 - 1
B = img(row1(1) : row2(1), :, :);
G = img(row1(2) : row2(2), :, :);
R = img(row1(3) : row2(3), :, :);
ColorImg(:,:,1) = R;
ColorImg(:,:,2) = G;
ColorImg(:,:,3) = B;
subplot(1, 2, 2);
imshow(ColorImg)
  4 Kommentare
Raja Bose
Raja Bose am 3 Sep. 2020
Finally got it............ ONLY THIS ONE BELOW WORKED:
img = imread('image.jpg');
[r,c] = size(img);
rr=r/3;
B=imcrop(img,[1,1,c,rr-1]);
G=imcrop(img,[1,1*rr+1,c-1,rr-1]);
R=imcrop(img,[1,2*rr+1,c-1,rr-1]);
ColorImg=cat(3,R,G,B)
imshow(ColorImg)
Image Analyst
Image Analyst am 3 Sep. 2020
Not sure what got printed out for you when you ran my original code because you chose not to share it, but I just copied and pasted it and ran it with the poster's original image, and it ran fine:
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
img = imread('image.jpeg');
subplot(1, 2, 1);
imshow(img);
[rows, columns, numberOfColorChannels] = size(img)
fprintf('Rows = %d, which means %f rows per image.\n', rows, rows/3);
row1 = [1, rows/3+1, 2*rows/3+1]
row2 = row1 + rows/3 - 1
B = img(row1(1) : row2(1), :, :);
G = img(row1(2) : row2(2), :, :);
R = img(row1(3) : row2(3), :, :);
ColorImg(:,:,1) = R;
ColorImg(:,:,2) = G;
ColorImg(:,:,3) = B;
subplot(1, 2, 2);
imshow(ColorImg)
fprintf('Done running %s.m ...\n', mfilename);
Here is what it prints out:
rows =
1023
columns =
400
numberOfColorChannels =
1
Rows = 1023, which means 341.000000 rows per image.
row1 =
1 342 683
row2 =
341 682 1023
Now let's look at your code. I added some lines to print out the rectangles and sizes of the images:
img = imread('image.jpeg');
[r,c] = size(img)
rr=r/3
[1,1,c,rr-1]
B=imcrop(img,[1,1,c,rr-1]);
whos B
[1,1*rr+1,c-1,rr-1]
G=imcrop(img,[1,1*rr+1,c-1,rr-1]);
whos G
[1,2*rr+1,c-1,rr-1]
R=imcrop(img,[1,2*rr+1,c-1,rr-1]);
whos R
ColorImg=cat(3,R,G,B);
imshow(ColorImg);
Look at the third element of your rectangle.
r =
1023
c =
400
rr =
341
ans =
1 1 400 340
Name Size Bytes Class Attributes
B 341x400 136400 uint8
ans =
1 342 399 340
Name Size Bytes Class Attributes
G 341x400 136400 uint8
ans =
1 683 399 340
Name Size Bytes Class Attributes
R 341x400 136400 uint8
For the first imcrop it's c (400) and for the second two crops it's c-1 (399). The third element is the width, which is the number of columns to crop out. Therefore your 3 images do not all have the same width and they shouldn't be able to be concatenated. But it did, and all the 3 images are the same size. Why is that? Too complicated to explain so I'll just say it's a quirk of imcrop where it wants a half pixel outside the region you want and doesn't give you the number of rows and columns you expect. Look at the sizes of your arrays. They're 400 rows tall, not 401 like they should be.
Try this:
img2 = randi(99, 5,7)
c = imcrop(img2, [4, 3, 3, 2]) % 3 columns and 2 rows? No! 4 columns and 3 rows!
img2 =
92 40 22 7 20 77 18
47 19 22 84 61 76 73
26 85 52 68 54 42 53
43 58 43 14 17 6 26
70 37 74 85 1 58 91
c =
68 54 42 53
14 17 6 26
85 1 58 91
But if the bottom of the rectangle goes below the image, it only takes up until the last line of the image.
Plus since you did not ask size for the number of color channels (a dangerous and not robust way to do it), your c is really the number of columns times the number of color channels. Though it fortunately didn't matter in this case because it was a gray scale image.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by