Thursday, February 17, 2022

Python-opencv image processing

 This is a demo of image processing in python/opencv 

Edit: I realized I made a mistake in the math description. Sinx was really tangent of the angle. The range for sin is -1 to +1 and which is why I wanted to use it. But since I found that tan variables are easily available I went for the tangent. The problem with tan is it's range goes from minus infinity to plus Infinity and you will run into divide by zero error unless you handle it before you calculate the tangent.  That's why I have greater than zero check.

I have added bookmarks in the description on youtube as below.

And here is the python code I used. It's very experimental but it will work for anyone who wants to test/prototype.

import cv2, sys, numpy, os, math, globals, subprocess

webcam = cv2.VideoCapture(1)

count = 1


(_, im) =

height = im.shape[0]

width = im.shape[1]


while 1:

 (_, im) =

 cv2.resizeWindow('Source', int(width+200) ,int(height))

 hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)

 lower_red = numpy.array([0,200,0])

 upper_red = numpy.array([20,255,255])

 mask = cv2.inRange(hsv, lower_red, upper_red)

 res = cv2.bitwise_and(im, im, mask=mask)

 gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)

 blurred = cv2.GaussianBlur(gray,(11,11),0)

 edge = cv2.Canny(im,75,100)

# cv2.imshow("Edge1", edge)

 edge = cv2.Canny(blurred,75,100)

# cv2.imshow("Edge", edge)

 linesP = cv2.HoughLinesP(edge, 1, numpy.pi / 180, 50, None, 10, 10)

 if linesP is not None:

  for i in range(0, len(linesP)):

   l = linesP[i][0]

   if ((l[3] - l[1]) > 0):

    sinx = ((l[2] - l[0]) / (l[3] - l[1]))

    if (sinx < -1) or (sinx > 1):

     cv2.line(im, (l[0], l[1]), (l[2], l[3]), (0,255,255), 5),(globals.mousexmove,globals.mouseymove),10,(255, 255, 0),3)

 cv2.imshow("masked", res)

 cv2.imshow("Source", im)

# cv2.imshow("Gray", gray)

# cv2.imshow("Edge", edge)

 cv2.imshow("Blur", blurred)

 count += 1

 key = cv2.waitKey(10)

 if key == 27:


The power surfing project

  01-Apr-2023: Sometime ago I had posted a video of a 400w trolling motor on my paddle board. It's a good option for a leisurely stroll ...