![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/712072/image.png)
Need help regarding image processing
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Arghya Pathak
le 14 Août 2021
Modifié(e) : Arghya Pathak
le 16 Août 2021
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.
0 commentaires
Réponse acceptée
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/712072/image.png)
Adapt as needed and report back.
7 commentaires
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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Explore and Edit Images with Image Viewer App dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!