Draw horizontal lines using for loop on set of images

조회 수: 4(최근 30일)
SS
SS 2019년 12월 11일
답변: Guillaume 2019년 12월 12일
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
Guillaume
Guillaume 2019년 12월 11일
Have you got the Computer Vision Toolbox? This would make this task very easy.

댓글을 달려면 로그인하십시오.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Guillaume
Guillaume 2019년 12월 12일
" 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.

Community Treasure Hunt

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

Start Hunting!

Translated by