draw lines and distances on image opencv python


user 300058

I'm having a problem with this: I can't draw a line on an image with a certain color, nor can I find out the distance to the place. Help make it look like the image below:image

my code:

import cv2
import numpy as np
from PIL import ImageGrab



    while True:
        screen = np.array(ImageGrab.grab(bbox=(0,40,800,640)))
        rgb_screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)

        lower = np.array([72, 160, 160])
        upper = np.array([112, 249, 249])


        mask = cv2.inRange(rgb_screen, lower, upper)
        output = cv2.bitwise_and(rgb_screen, rgb_screen, mask=mask)

        cv2.imshow('window', output)
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break
Kafka

There's nothing I can do because I don't have your original image. But you can read my code and maybe get an idea. For distance, I don't know what you mean, so I gave an example on how to get the distance from the top left corner to the bottom left corner. You can apply other points or apply it as a ratio according to your requirements.

import cv2
import numpy as np

img = cv2.imread('untitled.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, threshold = cv2.threshold(gray,150,255,cv2.THRESH_BINARY)
im, contours, hierarchy = cv2.findContours(threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
area = sorted(contours, key=cv2.contourArea, reverse=True)
c = area[0]
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
print(box)
box = np.int0(box)
cv2.drawContours(img,[box],0,(0,0,255),2)
extreme_left = tuple(c[c[:, :, 0].argmin()][0])
extreme_top = tuple(c[c[:, :, 1].argmin()][0])
x1 = box[1,0]
y1 = box[1,1]
x2 = box[0,0]
y2 = box[0,1]
distance = np.sqrt( (x1 - x2)**2 + (y1 - y2)**2 )
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'Distance: '+str(distance),(1,300), font, 0.5,(255,255,255),2,cv2.LINE_AA)
cv2.circle(img, (x2,y2), 5, (255, 0, 0), -1)
cv2.circle(img, (x1,y1), 5, (255, 0, 0), -1)
cv2.imshow('image', img)

enter image description here

Related


How to draw lines on an image in OpenCV?

Rahul: If I have polar coordinates of a line, how can I draw it on an image in OpenCV and python? LineThe function takes 2 points, but only draws line segments. I want to draw a line from one edge of an image to the other. Robert Caspary: Just count 2 points.

How to draw lines on an image in OpenCV?

Rahul: If I have polar coordinates of a line, how can I draw it on an image in OpenCV and python? LineThe function takes 2 points, but only draws line segments. I want to draw a line from one edge of an image to the other. Robert Caspary: Just count 2 points.

How to draw lines on an image in OpenCV?

Rahul: If I have polar coordinates of a line, how can I draw it on an image in OpenCV and python? LineThe function takes 2 points, but only draws line segments. I want to draw a line from one edge of an image to the other. Robert Caspary: Just count 2 points.

How to find and draw the outer lines of an image in OpenCV?

Mr Programmer As i process the image(BEFORE) then i need its outer border and draw red line in openCV android (like AFTER image), If anyone knows about it, please let me know about advanced tanks. Costantino Grana This is a classic application of cv::findConto

How to find and draw the outer lines of an image in OpenCV?

Mr Programmer As i process the image(BEFORE) then i need its outer border and draw red line in openCV android (like AFTER image), If anyone knows about it, please let me know about advanced tanks. Costantino Grana This is a classic application of cv::findConto

How to draw perfect lines in python using opencv

Trojan War I am trying to draw perfect lines using mouse events by clicking and dragging the mouse. The problem is that multiple lines are printed when plotting. Here is the code I've been testing. import cv2 import numpy as np drawing = False x1,y1 = -1,-1

Append lines to image in OpenCV Python

soup Suppose I have an image with dimensions 40 x 39. I need to add a row to this image so that the final image will be 40 x 40 in size. and how to do it horizontally and vertically? I tried, img = cv2.imread('image.jpg') blank_image = np.zeros((1, 40, 3), np.

Draw bidirectional arrow on image using opencv python

muaz faz I want to draw two arrows between two points using opencv. I have a function for single arrow graph as below import cv2 img = cv2.imread('image.jpg', cv2.IMREAD_COLOR) pt1 = (x1, y1) pt2 = (x2, y2) cv2.arrowedLine(img_, pt1, pt2, (0,0,255), 5) cv2.im

Draw bidirectional arrow on image using opencv python

muaz faz I want to draw two arrows between two points using opencv. I have a function for single arrow graph as below import cv2 img = cv2.imread('image.jpg', cv2.IMREAD_COLOR) pt1 = (x1, y1) pt2 = (x2, y2) cv2.arrowedLine(img_, pt1, pt2, (0,0,255), 5) cv2.im

Draw bidirectional arrow on image using opencv python

muaz faz I want to draw two arrows between two points using opencv. I have a function for single arrow graph as below import cv2 img = cv2.imread('image.jpg', cv2.IMREAD_COLOR) pt1 = (x1, y1) pt2 = (x2, y2) cv2.arrowedLine(img_, pt1, pt2, (0,0,255), 5) cv2.im

Draw bidirectional arrow on image using opencv python

muaz faz I want to draw two arrows between two points using opencv. I have a function for single arrow graph as below import cv2 img = cv2.imread('image.jpg', cv2.IMREAD_COLOR) pt1 = (x1, y1) pt2 = (x2, y2) cv2.arrowedLine(img_, pt1, pt2, (0,0,255), 5) cv2.im

Draw bidirectional arrow on image using opencv python

muaz faz I want to draw two arrows between two points using opencv. I have a function for single arrow graph as below import cv2 img = cv2.imread('image.jpg', cv2.IMREAD_COLOR) pt1 = (x1, y1) pt2 = (x2, y2) cv2.arrowedLine(img_, pt1, pt2, (0,0,255), 5) cv2.im

Remove horizontal lines in image (OpenCV, Python, Matplotlib)

Lucian Using the following code, I can remove the horizontal lines in the image. See results below. import cv2 from matplotlib import pyplot as plt img = cv2.imread('image.png',0) laplacian = cv2.Laplacian(img,cv2.CV_64F) sobelx = cv2.Sobel(img,cv2.CV_64F,1,

Separate lines and circles in an image using OpenCV in python

strangeness I'm trying to separate lines and circles instead of connecting some circles with curves. I tried using the contour to find the circle, but the contour included the line, so I also tried skeletonizing the image to see if the connection between the c

Remove horizontal lines in image (OpenCV, Python, Matplotlib)

Lucian Using the following code, I can remove the horizontal lines in the image. See results below. import cv2 from matplotlib import pyplot as plt img = cv2.imread('image.png',0) laplacian = cv2.Laplacian(img,cv2.CV_64F) sobelx = cv2.Sobel(img,cv2.CV_64F,1,

Remove horizontal lines in image (OpenCV, Python, Matplotlib)

Lucian Using the following code, I can remove the horizontal lines in the image. See results below. import cv2 from matplotlib import pyplot as plt img = cv2.imread('image.png',0) laplacian = cv2.Laplacian(img,cv2.CV_64F) sobelx = cv2.Sobel(img,cv2.CV_64F,1,

Separate lines and circles in an image using OpenCV in python

strangeness I'm trying to separate lines and circles instead of connecting some circles with curves. I tried using the contour to find the circle, but the contour included the line, so I also tried skeletonizing the image to see if the connection between the c

Separate lines and circles in an image using OpenCV in python

strangeness I'm trying to separate lines and circles instead of connecting some circles with curves. I tried using the contour to find the circle, but the contour included the line, so I also tried skeletonizing the image to see if the connection between the c

Remove horizontal lines in image (OpenCV, Python, Matplotlib)

Lucian Using the following code, I can remove the horizontal lines in the image. See results below. import cv2 from matplotlib import pyplot as plt img = cv2.imread('image.png',0) laplacian = cv2.Laplacian(img,cv2.CV_64F) sobelx = cv2.Sobel(img,cv2.CV_64F,1,

Remove horizontal lines in image (OpenCV, Python, Matplotlib)

Lucian Using the following code, I can remove the horizontal lines in the image. See results below. import cv2 from matplotlib import pyplot as plt img = cv2.imread('image.png',0) laplacian = cv2.Laplacian(img,cv2.CV_64F) sobelx = cv2.Sobel(img,cv2.CV_64F,1,

Separate lines and circles in an image using OpenCV in python

strangeness I'm trying to separate lines and circles instead of connecting some circles with curves. I tried using the contour to find the circle, but the contour included the line, so I also tried skeletonizing the image to see if the connection between the c

Cluster bounding boxes and draw lines on them (OpenCV, Python)

Lucian With this code, I create some bounding boxes around the characters in the image below: import csv import cv2 from pytesseract import pytesseract as pt pt.run_tesseract('bb.png', 'output', lang=None, boxes=True, config="hocr") # To read the coordinates

Cluster bounding boxes and draw lines on them (OpenCV, Python)

Lucian With this code, I create some bounding boxes around the characters in the image below: import csv import cv2 from pytesseract import pytesseract as pt pt.run_tesseract('bb.png', 'output', lang=None, boxes=True, config="hocr") # To read the coordinates

Cluster bounding boxes and draw lines on them (OpenCV, Python)

Lucian With this code, I create some bounding boxes around the characters in the image below: import csv import cv2 from pytesseract import pytesseract as pt pt.run_tesseract('bb.png', 'output', lang=None, boxes=True, config="hocr") # To read the coordinates

opencv - draw contours in an image

learner I am trying to draw contours around an image. I can see that contours are found, but cannot draw contours. The color of the outline appears to be one of two (black and white). import cv2 import numpy as np import matplotlib.pyplot as plt from skimage i

opencv - draw contours in an image

learner I am trying to draw contours around an image. I can see that contours are found, but cannot draw contours. The color of the outline appears to be one of two (black and white). import cv2 import numpy as np import matplotlib.pyplot as plt from skimage i

opencv - draw contours in an image

learner I am trying to draw contours around an image. I can see that contours are found, but cannot draw contours. The color of the outline appears to be one of two (black and white). import cv2 import numpy as np import matplotlib.pyplot as plt from skimage i

opencv - draw contours in an image

learner I am trying to draw contours around an image. I can see that contours are found, but cannot draw contours. The color of the outline appears to be one of two (black and white). import cv2 import numpy as np import matplotlib.pyplot as plt from skimage i