How to add/introduce text and scratches in a gray scale image in matlab? I need to generate random scratches at any postion within the image of my interest.Please Tell me in an easy and lucid way. Also provide some basic codes not advanced one, which is easily understandable.

 Réponse acceptée

Image Analyst
Image Analyst le 14 Août 2021
If you have the Computer Vision Toolbox, you can use insertText() to burn text into the image.
To introduce scratches it depends if they're straight or curved and if they have variable thickness or not. If they're just simple straight lines of one pixel, then simply use rand() to get a point, then again to get a point a distance away from it. You can use rand() to get random numbers for both the angle and length of the scratch. Then use polyfit() to get the equation of the line and polyfit() to get all the coordinates in between those endpoints. Then just loop over those burning some value, such as zero into it.
Is this your homework? Otherwise if your attempts over a half hour don't work let me know and I'll help. It's going to be something like
% Demo by Image Analyst to generate straight line not anti-aliased lines
% (scratches) in a gray scale image.
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
grayImage = imread('cameraman.tif');
% 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
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(1, 2, 1);
imshow(grayImage, []);
axis('on', 'image');
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
numScratches = 200
for s = 1 : numScratches
xy1 = rand(1,2);
x1 = columns * xy1(1);
y1 = rows * xy1(2);
lineLength = 100 * rand(1);
angle = 360 * rand(1);
x2 = x1 + lineLength * cosd(angle);
y2 = y1 + lineLength * sind(angle);
coefficients = polyfit([x1,x2], [y1,y2], 1);
% Get lots of x so we don't have "gaps" in line.
allX = linspace(x1, x2, rows);
allY = round(polyval(coefficients, allX));
% Now round x
allX = round(allX);
for k = 1 : length(allY)
if allY(k) > rows || allX(k) > columns || allY(k) < 1 || allX(k) < 1
continue; % Skip pixels outside of image.
end
grayImage(allY(k), allX(k)) = 0;
end
subplot(1, 2, 2);
imshow(grayImage); % Display your original image.
drawnow;
end
caption = sprintf('With %d scratches in it', numScratches);
title(caption, 'FontSize', fontSize);
Adapt as needed and report back.

7 commentaires

Arghya Pathak
Arghya Pathak le 14 Août 2021
Modifié(e) : Arghya Pathak le 14 Août 2021
Thank you very much for answering. Actually I need to create curve ,straight both type of scratches of variable thickness. I want to introduce some scratches,missing pixel elements in the gray scale image. I will run this code and inform you tomorrow.
Image Analyst
Image Analyst le 14 Août 2021
You might be more realistic if you segment out some actual scratches from real images, then you'll have a "library" of realistic scratches. Then use random numbers to vary the location, rotation, and size of them before pasting them onto the image. If I had this as a real project (as opposed to just some play or homework project), that's what I'd do.
Arghya Pathak
Arghya Pathak le 15 Août 2021
Actually I am not that much accustomed with programming so I need a basic and easy method.
Image Analyst
Image Analyst le 15 Août 2021
Alright, then I guess since you're not that experienced with programming you'll just have to use the basic easy method I gave you code for. Maybe someday you'll be able to do it the more realistic way I mentioned. Thanks for Accepting the answer.
Arghya Pathak
Arghya Pathak le 15 Août 2021
Modifié(e) : Arghya Pathak le 15 Août 2021
Thanks for you cooperation. Can you please suggest me how I can get expertise in writing the image processing programs in matlab for different applications like image inpainting,noise removal etc. Is there any good source to learn the programs from basic and get experienced ?? Actually I have not found a good source till now, I have searched a lot but get only elementary idea not in detail. I want to learn the step wise detail of those standard programming and the theory related with each of the steps. So that I can be able to modify them according to my need.Please help me.
Image Analyst
Image Analyst le 15 Août 2021
My favorite book is the "Image Processing Handbook" by John Russ. A huge variety of examples from a wide variety of fields. Covers a lot of different operations. Very light on the math so it's easy to follow.
Another book is by Steve Eddins and uses MATLAB to go through examples.
I think both are still available on Amazon.com.
Another good book available online is
Arghya Pathak
Arghya Pathak le 16 Août 2021
Modifié(e) : Arghya Pathak le 16 Août 2021
Okay.Thank you very much for your support.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by