# Draw horizontal lines using for loop on set of images

조회 수: 13 (최근 30일)
SS 2019년 12월 11일
답변: Sakib 2024년 7월 4일 3:03
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이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Guillaume 2019년 12월 11일
Have you got the Computer Vision Toolbox? This would make this task very easy.

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

### 채택된 답변

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
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이전 댓글 1개 표시이전 댓글 1개 숨기기
Guillaume 2019년 12월 12일
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 2019년 12월 12일
편집: SS 2019년 12월 12일
Hi, I am sorry. Somehow, missed to reply back. No, I don't have that tool box.

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

### 추가 답변 (2개)

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.
##### 댓글 수: 0이전 댓글 -2개 표시이전 댓글 -2개 숨기기

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

Sakib 2024년 7월 4일 3:03
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
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

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

### 카테고리

Help CenterFile Exchange에서 Image Arithmetic에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by