How to assign character to foreground and background in image?
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Currently, I have an image with single letter in the middle it is white and background is black ,in grayscale. I managed to store them in different variable. But now, I would like to display them by cover the whole foreground and background with different character. For example, 'b' for the pixels of the image for background,while 'o' for the pixels of foreground. I try to search and try on my own but not successful.Therefore,I am here to ask for guide.
%create blank image
w = 150;
h = 150;
blankImage= 255*ones(w,h,3,'uint8');
%position of the letter in the empty cell
position_x = (w+1)/2;
position_y = (h+1)/2;
% varying the font size, start from 10 to 16
font_start = 58;
font_end = 64;
num_fontsA = font_start:2:font_end;
% get the number of fonts
numImagesA = length(num_fontsA)
% create a cell array with number of fonts to fill in the image in next step
A = cell(1, numImagesA);
% for loop to create letter 'A'
% grayscale
% store into the cell array
for i=1:numImagesA
for font_size = num_fontsA(i)
img= insertText(blankImage,[position_x position_y],'A','Font','Times New Roman','FontSize',font_size,'TextColor','black','BoxColor','w','BoxOpacity',0,'AnchorPoint','Center');
grayImage= rgb2gray(img);
BWImage = ~grayImage;
background = BWImage == 0;
foreground = ~background;
%testing ,but it failed
Newforegnd = foreground;
L = repmat(foreground==1,1,1,size(Newforegnd,3));
Newforegnd(L)='x';
figure('Name','Background and Object','NumberTitle','off');
montage({Newforegnd, foreground});
% figure;
% A{i} = BWImage;
% subplot(2,2,i);
% imshow(A{i});
% % imshow(background);
% A{i}= imnoise(img, 'gaussian',0, 0.01);
% subplot(2,2,i);
% imshow(A{i});
axis on;
title("A with font size " + font_size);
% title("A with font size " + font_size +" with noise 0.01 variance");
end
end
This is what I get from the background and foreground variable previously from help in Matlab forum.
4 Commenti
Image Analyst
il 20 Giu 2021
"This is the example from senior" <== Who or what is "senior".
If your professor did not want you to have any additional information from him, then presumably he doesn't want us to give you any additional information either. So I guess you're on your own but I'm sure you can still use the help documentation. If your professor says it's OK to get help from the Answers forum, then let us know he says it's okay.
Risposta accettata
DGM
il 20 Giu 2021
What you describe sounds like a simple logical masking operation, but the image appears to be a mosaic instead. If the latter is indeed the intended result, consider that the size of your output image will be the product of the sizes of the pattern image and the tile image(s). Considering that the pattern image has two values, then a logical approach should be evident. It may be prudent to reduce the size of the tile images, or at least ensure they have common geometry before trying to assemble the mosaic.
Actually if I'm going to offer an example, I might as well move it to an answer...
% generate some letter images. textim() is from MIMT on the FEX
a = textim('A','ibm-vga-8x8');
b = textim('B','ibm-vga-8x8');
c = textim('C','ibm-vga-8x8');
% assemble mosaic using kronecker product
d = kron(a,b) + kron(1-a,c);
% note 1-a denotes inversion. a is of class double.
% if a were uint8(), you'd need to use a different value.
% you can also use imcomplement()
These images are obviously resized for viewing purposes. As I said, the output size will be the product of the pattern and tile sizes.
5 Commenti
DGM
il 21 Giu 2021
If you want it to look like your picture (the font, etc) then you'd probably need to use insertText() or something similar. Textim() only has a selection of relatively compact legacy bitmap system/bios fonts -- nothing modern. This shows all the fonts:
I'm not familiar with what insertText offers since I don't have CVT. If it's not to your liking, I'm pretty sure there are some other text-image converters on the File Exchange.
Più risposte (1)
Image Analyst
il 20 Giu 2021
For what it's worth, here's perhaps a start:
% Demo by Image Analyst
clc; % Clear the command window.
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;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = pwd;
baseFileName = 'ocr.png';
grayImage = imread(baseFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Extract the red channel (so the magenta lines will be white).
grayImage = grayImage(:, :, 1);
end
% Get rid of junk in the lower left by erasing the leftmost 7 columns
grayImage(:, 1:7) = 255;
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
% Take histogram
subplot(2, 2, 2);
edges = 0 : 256;
histObject = histogram(grayImage, edges)
grid on;
title('Histogram', 'FontSize', fontSize);
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
% Binarize
mask = grayImage < 230;
subplot(2, 2, 3);
imshow(mask, []);
axis('on', 'image');
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
% Get horizontal and vertical profiles.
horizontalProfile = mean(grayImage, 1);
verticalProfile = mean(grayImage, 2);
subplot(2, 2, 4);
plot(horizontalProfile, 'b-', 'LineWidth', 1);
hold on;
grid on;
plot(verticalProfile, 'r-', 'LineWidth', 1);
legend('Horizontal Profile', 'Vertical Profile');
drawnow;
% Find the centroids of each "lane" of characters
% First find vertical lanes by finding horizontal dividing points.
propsh = regionprops(horizontalProfile > 230, 'Centroid');
xy = vertcat(propsh.Centroid)
xh = unique(xy(:, 1)')
subplot(2, 2, 3);
hold on;
for k = 1 : length(xh)
xline(xh(k), 'Color', 'r');
end
% Next find horizontal lanes by finding vertical dividing points.
propsv = regionprops(verticalProfile > 230, 'Centroid');
xy = vertcat(propsv.Centroid)
yv = unique(xy(:, 2)')
subplot(2, 2, 3);
hold on;
for k = 1 : length(yv)
yline(yv(k), 'Color', 'y');
end
% Round centroids to the nearest line or column
xh = round(xh);
yv = round(yv);
figure;
numRows = length(yv);
numColumns = length(xh);
% Extract each subimage
plotIndex = 1;
for col = 1 : length(xh)-1
col1 = xh(col);
col2 = xh(col+1) - 1;
fprintf('Extracting columns %d to %d.\n', col1, col2);
for row = 1 : length(yv) - 1
row1 = yv(row);
row2 = yv(row+1)-1;
subImage = grayImage(row1:row2, col1:col2);
subplot(numRows, numColumns, plotIndex);
imshow(subImage, []);
plotIndex = plotIndex + 1;
drawnow;
end
end
g = gcf;
g.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
4 Commenti
Image Analyst
il 23 Giu 2021
You're welcome. If you have more specific questions, post them and attach your image. Otherwise, please "Accept" the best answer (you can only accept one but you can vote for more than one).
Vedere anche
Categorie
Scopri di più su Image Processing Toolbox in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!