MATLAB Image Processing _ directly operate on the pixel for color conversion, matlab Image Processing
Requirement
Directly operate the pixels of the RGB image and perform color-related operations.
To master this, you must be familiar with matrix operations in MATLAB, especially the operations of the entire row and the entire column.
For example:
J = [1 2 3; 4 5 6; 7 8 9]; -- a matrix of three rows and three columns is defined here.
J [:, 1] = 0; -- directly operates the 1st columns of each row in the J matrix. At this time, J = [0 2 3; 0 5 6; 0 8 9]
Other operations, such as the row operation usage, are not described in detail.
Next we will perform a direct operation on an image and change the red part to blue.
The Code is as follows:
% BY SCOTT % red2blue% change red to blueclear all;clc;rgb = imread('red2blue.png');figure;imshow(rgb);R=rgb(:,:,1); %redG=rgb(:,:,2); %greenB=rgb(:,:,3); %blue[x,y,z]=size(rgb);for i=1:x for j=1:y if( (R(i,j) >= 180) && (R(i,j) <=255) && (G(i,j) <50) && (B(i,j) <50) ) R(i,j) = 0; G(i,j) = 162; B(i,j) = 232; end endend% in this way% blue(:,:,1)=R(:,:);% blue(:,:,2)=G(:,:);% blue(:,:,3)=B(:,:);% another wayfor i=1:x for j=1:y blue(i,j,1) = R(i,j); blue(i,j,2) = G(i,j); blue(i,j,3) = B(i,j); endendfigure;imshow(blue);
Running result:
Before conversion:
After conversion: