Draw horizontal lines using for loop on set of images

6 Ansichten (letzte 30 Tage)
SS
SS am 11 Dez. 2019
Beantwortet: Guillaume am 12 Dez. 2019
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 Kommentar
Guillaume
Guillaume am 11 Dez. 2019
Have you got the Computer Vision Toolbox? This would make this task very easy.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stijn Haenen
Stijn Haenen am 11 Dez. 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 Kommentare
Guillaume
Guillaume am 12 Dez. 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.
SS
SS am 12 Dez. 2019
Bearbeitet: SS am 12 Dez. 2019
Hi, I am sorry. Somehow, missed to reply back. No, I don't have that tool box.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Guillaume
Guillaume am 12 Dez. 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.

Kategorien

Mehr zu Convert Image Type finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by