Life2Coding
How to use Clipline Function in OpenCV Python

This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply them in various aspects using some great examples. Then the output will be visualized along with the comparisons.

We will also discuss the basic of image processing and provide the detail explanation related to the OpenCV functions.

Requirements:

First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV with Python 3

Goals:

The goal is to get clip line points of lines using OpenCV python.

Documentation:

line()

img=cv.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]])

Draws a line segment connecting two points.

Parameters
img Image.
pt1 First point of the line segment.
pt2 Second point of the line segment.
color Line color.
thickness Line thickness.
lineType Type of the line. See LineTypes.
shift Number of fractional bits in the point coordinates.

rectangle()

img=cv.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]])
img=cv.rectangle(img, rec, color[, thickness[, lineType[, shift]]])
img=cv.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]])
img=cv.rectangle(img, rec, color[, thickness[, lineType[, shift]]])

Draws a simple, thick, or filled up-right rectangle.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
img Image.
pt1 Vertex of the rectangle.
pt2 Vertex of the rectangle opposite to pt1 .
color Rectangle color or brightness (grayscale image).
thickness Thickness of lines that make up the rectangle. Negative values, like FILLED, mean that the function has to draw a filled rectangle.
lineType Type of the line. See LineTypes
shift Number of fractional bits in the point coordinates.

clipLine()

retval, pt1, pt2=cv.clipLine(imgRect, pt1, pt2)
retval, pt1, pt2=cv.clipLine(imgRect, pt1, pt2)
retval, pt1, pt2=cv.clipLine(imgRect, pt1, pt2)

Clips the line against the image rectangle.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
imgSize Image size. The image rectangle is Rect(0, 0, imgSize.width, imgSize.height) .
pt1 First line point.
pt2 Second line point.
Parameters
imgSize Image size. The image rectangle is Rect(0, 0, imgSize.width, imgSize.height) .
pt1 First line point.
pt2 Second line point.
Parameters
imgRect Image rectangle.
pt1 First line point.
pt2 Second line point.

imshow()

None=cv.imshow(winname, mat)

Displays an image in the specified window.

Parameters
winname Name of the window.
mat Image to be shown.

waitKey()

retval=cv.waitKey([, delay])

Waits for a pressed key.

Parameters
delay Delay in milliseconds. 0 is the special value that means “forever”.

destroyAllWindows()

None=cv.destroyAllWindows()

Destroys all of the HighGUI windows.

Steps:

  • Create a 512×512 black image using np.zeros()
  • Define some colors for drawing some lines and rectangle
  • Draw a line using cv2.line()
  • Draw a rectangle using cv2.rectangle()
  • Get the clip line points for a certain segment of lines using cv2.clipLine()
  • Draw a line using the clip line points using cv2.line()
  • Display the image using cv2.imshow()
  • Wait for keyboard button press using cv2.waitKey()
  • Exit window and destroy all windows using cv2.destroyAllWindows()

Example Code:

import numpy as np
import cv2
if __name__ == '__main__':

    #create a 512x512 black image
    image=np.zeros((512,512,3),np.uint8)
    
    colors={'red':(0,0,255),'blue':(255,0,0),'yellow':(0,255,255)}
    cv2.line(image, (0, 0), (300, 300), colors['red'], 3)
    cv2.rectangle(image, (0, 0), (100, 100), colors['blue'], 3)
    ret, p1, p2 = cv2.clipLine((0, 0, 100, 100), (0, 0), (300, 300))
    if ret:
        cv2.line(image, p1, p2, colors['yellow'], 3)

    cv2.imshow("ClipLine",image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

Output:

5dbf894eae692 How to use Clipline Function in OpenCV Python
life2coding_icon [] How to use Clipline Function in OpenCV Python

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.