Color the the image

4 Ansichten (letzte 30 Tage)
Tu Nguyen
Tu Nguyen am 23 Feb. 2022
Kommentiert: Voss am 28 Feb. 2022
Hi all,
How can I do the script for the 2 input images to produce the output image?

Akzeptierte Antwort

Voss
Voss am 25 Feb. 2022
Bearbeitet: Voss am 25 Feb. 2022
First of all, let me say Go Coogs.
Second, here's some code you can use. This works from the input image provided, i.e., it first extracts the rectangular black regions containing the letters U and H, then combines them into a new image and colors the letters red.
(If instead you started from two input images just containing the rectangular regions containing the letters, then the first part of this where the U and H are extracted could be skipped.)
im_input = imread('input.png');
[m,n,p] = size(im_input);
% first, have to extract the rectangles (almost squares - so I'll call
% them squares) containing the letters from the input image:
% find the upper-left corner of the U square by finding the first black
% pixel in the image:
[u_start_row,u_start_col] = find(all(im_input == 0,3),1)
u_start_row = 28
u_start_col = 22
% find the lower-right corner of the U square by finding the last black
% pixel in the left half of the image:
[u_end_row,u_end_col] = find(all(im_input(:,1:floor(n/2),:) == 0,3),1,'last')
u_end_row = 139
u_end_col = 140
% find the upper-left corner of the H square by finding the first black
% pixel in the right half of the image, then adjust the column index
% accordingly:
[h_start_row,h_start_col] = find(all(im_input(:,floor(n/2)+1:end,:) == 0,3),1);
h_start_row
h_start_row = 28
h_start_col = h_start_col+floor(n/2)
h_start_col = 181
% find the lower-right corner of the H square by finding the last black
% pixel in the image:
[h_end_row,h_end_col] = find(all(im_input == 0,3),1,'last')
h_end_row = 139
h_end_col = 299
% copy the black squares containing the letters to separate variables:
im_u = im_input(u_start_row:u_end_row,u_start_col:u_end_col,:);
im_h = im_input(h_start_row:h_end_row,h_start_col:h_end_col,:);
% show them:
imshow(im_u);
imshow(im_h);
% make sure the "squares" are the same size
% (if not, then one or both needs to be adjusted by adding black rows and/or
% columns to match the other one's size, but here they are the same size,
% luckily.)
assert(isequal(size(im_u),size(im_h)))
% now put them together and change the letters into red:
% first, have to decide how to consider whether a pixel is part of a
% letter (2 options):
% option 1: to use the locations of all pixels that aren't black in the
% final output (i.e., include the grey borders around the letters):
% u_idx = ~all(im_u == 0,3);
% h_idx = ~all(im_h == 0,3);
% option 2: to use the locations of the white pixels only in the final
% output image (i.e., excluding the grey borders around the letters):
% I think this is how the actual output image is.
u_idx = all(im_u == 255,3);
h_idx = all(im_h == 255,3);
% finally, construct the output image. first as a cell array containing a
% 2D matrix for each channel (R,G,B):
im_uh = num2cell(zeros(size(im_u),'uint8'),[1 2]);
% red color:
c = [255 0 0];
% for each channel ...
for ii = 1:p
% ... make the pixels red where they're part of a letter:
im_uh{ii}(u_idx | h_idx) = c(ii);
end
% combine the (R,G,B) cell array into a 3D matrix:
im_uh = cat(3,im_uh{:});
% show the result:
imshow(im_uh); % Go Coogs!
  2 Kommentare
Tu Nguyen
Tu Nguyen am 28 Feb. 2022
Thank you so much
Voss
Voss am 28 Feb. 2022
You're welcome!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Mahesh Taparia
Mahesh Taparia am 28 Feb. 2022
Hi
This problem can be solve by adding the image of both the letter 'U' and 'H' and doing some color changing. At first, split the image from middle to 2 images of letter 'U' and 'H'. For example, consider the code below:
a=rgb2gray(imread('uh.png'));
sz = size(a);
u = a(:,1:sz(2)/2);
h = a(:,sz(2)/2+1:end);
Then add the images of both the letters and change the color as per requirement. Use 'insertText' function to add text inside the image, you can change the text color, box color, etc. For more information, you can refer its documentation here. For example, consider the below code
uh = u+h;
I = zeros(162,160,3);
I(:,:,1) = uh; % To keep red background, you can change the color as per need
I = insertText(I,[35,7],'Output 2: UH Red');
figure;imshow(I)
Hope it will help!

Kategorien

Mehr zu Image Processing Toolbox 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