Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 3-by-1.

5 次查看(过去 30 天)
%% whenever i run this code it says this same error however it worked with other matrices so why is it that it wont work for these 3.
SepiaMatrix=[0.393 0.769 0.189;0.349 0.686 0.168;0.272 0.534 0.131];
for i=1:m
for j=1:n
PixelColor=reshape(double(ImJPG(i,j,:)),3,1);
ImJPG_Sepia(i,j,:)=uint8(SepiaMatrix*PixelColor);
end;
end;
Unrecognized function or variable 'm'.
figure;
imshow(ImJPG_Sepia);
%% 6
RedMatrix=[1 0 0;0 0 0;0 0 0];
for i=1:m
for j=1:n
PixelColor=reshape(double(ImJPG(i,j,:)),3,1);
ImJPG_Red(i,j,:)=uint8(RedMatrix*PixelColor);
end;
end;
figure;
imshow(ImJPG_Red);
%%Q2:The transformation above makes it so only the red colors will show.
%% 7
PermuteMatrix=[0 0 1;0 1 0;1 0 0];
for i=1:m
for j=1:n
PixelColor=reshape(double(ImJPG(i,j,:)),3,1);
ImJPG_Permute(i,j,:)=uint8(PermuteMatrix*PixelColor);
end;
end;
figure;
imshow(ImJPG_Permute);
  2 个评论
Umar
Umar 2024-10-8

Hi @Jenny ,

To resolve the issue with the unrecognized variable m, you first need to define m and n based on the dimensions of the input image ImJPG. The variables m and n should represent the number of rows and columns of the image, respectively. Additionally, I will ensure that the code is structured properly and includes comments for clarity. Here is the updated and complete MATLAB code:

% Load the image
ImJPG = imread('your_image.jpg'); % Replace 'your_image.jpg' with your  
actual image file
[m, n, ~] = size(ImJPG); % Get the dimensions of the image
% Initialize the output images
ImJPG_Sepia = zeros(m, n, 3, 'uint8');
ImJPG_Red = zeros(m, n, 3, 'uint8');
ImJPG_Permute = zeros(m, n, 3, 'uint8');
% Sepia Transformation
SepiaMatrix = [0.393 0.769 0.189; 
             0.349 0.686 0.168; 
             0.272 0.534 0.131];
for i = 1:m
  for j = 1:n
      PixelColor = reshape(double(ImJPG(i,j,:)), 3, 1);
      ImJPG_Sepia(i,j,:) = uint8(SepiaMatrix * PixelColor);
  end
end
% Display the Sepia image
figure;
imshow(ImJPG_Sepia);
title('Sepia Image');
% Red Color Transformation
RedMatrix = [1 0 0; 
           0 0 0; 
           0 0 0];
for i = 1:m
  for j = 1:n
      PixelColor = reshape(double(ImJPG(i,j,:)), 3, 1);
      ImJPG_Red(i,j,:) = uint8(RedMatrix * PixelColor);
  end
end
% Display the Red image
figure;
imshow(ImJPG_Red);
title('Red Image');
% Permutation of Color Channels
PermuteMatrix = [0 0 1; 
               0 1 0; 
               1 0 0];
for i = 1:m
  for j = 1:n
      PixelColor = reshape(double(ImJPG(i,j,:)), 3, 1);
      ImJPG_Permute(i,j,:) = uint8(PermuteMatrix * PixelColor);
  end
end
% Display the Permuted image
figure;
imshow(ImJPG_Permute);
title('Permuted Color Channels Image');

So, in the above modified code snippet, it begins by loading an image using imread. Make sure to replace “your_image.jpg” with the actual path to your image file. The dimensions of the image are extracted using the size function, which provides the number of rows (m) and columns (n). Then, three output images (ImJPG_Sepia, ImJPG_Red, and ImJPG_Permute) are initialized to store the results of the transformations. They are initialized as zero matrices of the same size as the input image. A sepia filter is applied using a transformation matrix. Each pixel's color is reshaped and multiplied by the sepia matrix. This transformation isolates the red channel by using a matrix that zeroes out the green and blue channels. The color channels are permuted using a specified matrix to rearrange the RGB values. Each transformed image is displayed using imshow, with appropriate titles for clarity. This code should now run without errors, and you will be able to see the results of the various color transformations applied to your image.

Please see attached.

If you have any further questions or need additional modifications, feel free to ask!

Umar
Umar 2024-10-8
Hi @ Jenny,
Please let us know if your problem is resolved. We will be more happy to help you out if you still require further assistance.

请先登录,再进行评论。

回答(3 个)

Walter Roberson
Walter Roberson 2024-10-8
You initialized ImJPG_Sepia as an n by m by 1 matrix.

Umar
Umar 2024-10-8

Hi @Jenny ,

To resolve the issue with the unrecognized variable m, you first need to define m and n based on the dimensions of the input image ImJPG. The variables m and n should represent the number of rows and columns of the image, respectively. Additionally, I will ensure that the code is structured properly and includes comments for clarity. Here is the updated and complete MATLAB code:

% Load the image
ImJPG = imread('your_image.jpg'); % Replace 'your_image.jpg' with your  
actual image file
[m, n, ~] = size(ImJPG); % Get the dimensions of the image
% Initialize the output images
ImJPG_Sepia = zeros(m, n, 3, 'uint8');
ImJPG_Red = zeros(m, n, 3, 'uint8');
ImJPG_Permute = zeros(m, n, 3, 'uint8');
% Sepia Transformation
SepiaMatrix = [0.393 0.769 0.189; 
             0.349 0.686 0.168; 
             0.272 0.534 0.131];
for i = 1:m
  for j = 1:n
      PixelColor = reshape(double(ImJPG(i,j,:)), 3, 1);
      ImJPG_Sepia(i,j,:) = uint8(SepiaMatrix * PixelColor);
  end
end
% Display the Sepia image
figure;
imshow(ImJPG_Sepia);
title('Sepia Image');
% Red Color Transformation
RedMatrix = [1 0 0; 
           0 0 0; 
           0 0 0];
for i = 1:m
  for j = 1:n
      PixelColor = reshape(double(ImJPG(i,j,:)), 3, 1);
      ImJPG_Red(i,j,:) = uint8(RedMatrix * PixelColor);
  end
end
% Display the Red image
figure;
imshow(ImJPG_Red);
title('Red Image');
% Permutation of Color Channels
PermuteMatrix = [0 0 1; 
               0 1 0; 
               1 0 0];
for i = 1:m
  for j = 1:n
      PixelColor = reshape(double(ImJPG(i,j,:)), 3, 1);
      ImJPG_Permute(i,j,:) = uint8(PermuteMatrix * PixelColor);
  end
end
% Display the Permuted image
figure;
imshow(ImJPG_Permute);
title('Permuted Color Channels Image');

So, in the above modified code snippet, it begins by loading an image using imread. Make sure to replace “your_image.jpg” with the actual path to your image file. The dimensions of the image are extracted using the size function, which provides the number of rows (m) and columns (n). Then, three output images (ImJPG_Sepia, ImJPG_Red, and ImJPG_Permute) are initialized to store the results of the transformations. They are initialized as zero matrices of the same size as the input image. A sepia filter is applied using a transformation matrix. Each pixel's color is reshaped and multiplied by the sepia matrix. This transformation isolates the red channel by using a matrix that zeroes out the green and blue channels. The color channels are permuted using a specified matrix to rearrange the RGB values. Each transformed image is displayed using imshow, with appropriate titles for clarity. This code should now run without errors, and you will be able to see the results of the various color transformations applied to your image.

Please see attached.

If you have any further questions or need additional modifications, feel free to ask!


DGM
DGM 2024-10-8
编辑:DGM 2024-10-8
Just use imapplymatrix()
inpict = imread('peppers.png');
M = [0.393 0.769 0.189; 0.349 0.686 0.168; 0.272 0.534 0.131];
outpict = imapplymatrix(M,inpict);
imshow(outpict)
There are more basic ways, but this is succinct.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by