Draw horizontal lines using for loop on set of images
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi. I have around 5000 images in a folder, the names of the images are Combined_1, Combined_2, .... Combined_5000. I want to draw four horizontal lines at positions on all the images using a loop.
Line #1: (X1,Y1)=(0,800) px and (X2,Y2)=(1024,800)px
Line #2: (X1,Y1)=(0,700) px and (X2,Y2)=(1024,700)px
Line #3: (X1,Y1)=(0,600) px and (X2,Y2)=(1024,600)px
Line #4: (X1,Y1)=(0,500) px and (X2,Y2)=(1024,500)px
The coordinates are in pixel units. I would appreciate if, someone can help me with a detailed code with explanation which has options to change the line type, thickness and color.
1 commentaire
Guillaume
le 11 Déc 2019
Have you got the Computer Vision Toolbox? This would make this task very easy.
Réponse acceptée
Stijn Haenen
le 11 Déc 2019
Maybe this works:
Color=[255,255,0]; %color in 8bit, [255 255 0] is yellow
width=3; %width of the line
Line_Y_coor=[500 600 700 800]; %ycoordinates of the line
for i=1:5000
im_data=imread(sprintf('Combined_%g.jpg',i)); %load the images
for n=1:4
im_data(Line_Y_coor(n):Line_Y_coor(n)-1+width,:,1)=Color(1); %add the lines in RGB
im_data(Line_Y_coor(n):Line_Y_coor(n)-1+width,:,2)=Color(2);
im_data(Line_Y_coor(n):Line_Y_coor(n)-1+width,:,3)=Color(3);
end
imwrite(im_data,sprintf('Combined_%g.jpg',i)); %safe the images
end
3 commentaires
Guillaume
le 12 Déc 2019
As I asked in a comment to the question, have you got the Computer Vision toolbox? If so, this would be dramatically easier and would allow you to format the lines and allow lines at any angle.
The above code can only work for horizontal and vertical lines.
Plus de réponses (2)
Guillaume
le 12 Déc 2019
" I don't have [the computer vision] tool box"
Then you have several options, none of them ideal
- draw your image into a matlab figure (with imshow), plot your lines into the figure (with plot or line), then convert the figure back into an image (with getframe). That's the easiest in term of drawing lines and you can use all the line plotting options that matlab offer. The downside is that getframe is a pain to work with and doesn't typically return an image the same size as you started with. If that's not a problem though, that's the way to go
- See if there's something on the FileExchange
- Implement your own line drawing algorithm, Bresenham's line algorithm is a simple one. Wu's a better one. You'd have to implement it yourself and customise it to have dashes, line thickness, etc.
- If on windows, you could use .Net functions to draw on your image. You'd convert the image to a System.Drawing.Graphics object, use methods such as DrawLine and then convert back to an image.
- There's probably a Java equivalent to the .Net method. This would work on any OS, but I'm not familiar enough with Java to tell you what it is.
0 commentaires
Sakib
le 4 Juil 2024
Color=[255,255,0]; %color in 8bit, [255 255 0] is yellow
width=3; %width of the line
Line_Y_coor=[500 600 700 800]; %ycoordinates of the line
for i=1:5000
im_data=imread(sprintf('Combined_%g.jpg',i)); %load the images
for n=1:4
im_data(Line_Y_coor(n):Line_Y_coor(n)-1+width,:,1)=Color(1); %add the lines in RGB
im_data(Line_Y_coor(n):Line_Y_coor(n)-1+width,:,2)=Color(2);
im_data(Line_Y_coor(n):Line_Y_coor(n)-1+width,:,3)=Color(3);
end
imwrite(im_data,sprintf('Combined_%g.jpg',i)); %safe the images
end
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!