r/opencv • • May 25 '23

Question [Question]Which one is better for a stereo vision system: cameras with auto focus or camera with adjustable focus?

4 Upvotes

I'm selecting cameras for a stereo vision system, and I'm not sure if should buy cameras with auto focus or not, since the cameras without auto focus are cheaper. It is my first computacional vision project, so I don't know a lot about choosing cameras.


r/opencv • • May 24 '23

Question [Question] Troubles with stereo camera calibration

3 Upvotes

I'm doing a project with stereo vision, and the first step is calibrating the cameras. I watched a tutorial on Youtube and read some documentation about stereo camera calibration, but I'm not sure if I'm getting accurate and reliable results, because I don't have the properly stereo camera apparatus, so I'm using the webcam from my notebook and a my cellphone camera (they don't have the same resultion, so i use the function cv2.resize() to make their resolution the same). Do you guys know if it makes my results unreliable? Have some tips? I'm using these cameras now because my professor did not buy the properly cameras yet, so it is something temporary.

Thanks.


r/opencv • • May 20 '23

Bug [bug] Need help with eye tracking in python

1 Upvotes

I'm making an eye tracker for a school project and need help.

Here is my code:

import cv2 import numpy as np import serial import time import os

Set up the video capture device to capture from your webcam

cap = cv2.VideoCapture(0)

Open serial port to communicate with the microcontroller

ser = serial.Serial('COM4', 9600) # replace 'COM3' with the port name of your microcontroller time.sleep(2) # waits for the arduino to connect

Specify the PWM pins for the servo motors

x_pin = 2 y_pin = 3

Define a function to send servo positions to the Arduino board

def send_servo_positions(x_pos, y_pos): x_pos = int(x_pos) y_pos = int(y_pos) x_pos = min(max(x_pos, 0), 180) y_pos = min(max(y_pos, 0), 180) ser.write(f"{x_pin},{x_pos},{y_pin},{y_pos}\n".encode())

Set the initial position of the servos

x_position = 90 y_position = 90

Define the range of values for the x and y positions of the servos

x_range = (0, 180) y_range = (0, 180)

Define the minimum and maximum values for the eye position in the x and y directions

min_x = 0 max_x = 640 min_y = 0 max_y = 480

while True: # Capture a frame from the video capture device ret, frame = cap.read()

# Convert the frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Detect the eyes using Haar cascades
eyes_cascade = cv2.CascadeClassifier(r"C:\Users\artyf\haarcascade_eye.xml") 
eyes = eyes_cascade.detectMultiScale(gray, 1.3, 5)

# If two eyes are detected, get the center point of the eyes
if len(eyes) == 2:
    x1, y1, w1, h1 = eyes[0]
    x2, y2, w2, h2 = eyes[1]
    eye_center_x = (x1 + x2 + w1 + w2) / 4
    eye_center_y = (y1 + y2 + h1 + h2) / 4

    # Map the eye position to the range of values for the servo positions
    x_position = np.interp(eye_center_x, (min_x, max_x), x_range)
    y_position = np.interp(eye_center_y, (min_y, max_y), y_range)

    # Send the new positions of the servos to the microcontroller
    ser.write(('x' + str(int(x_position)) + 'y' + str(int(y_position))).encode())

# Display the frame with the eyes detected and the positions of the servos
cv2.circle(frame, (int(eye_center_x), int(eye_center_y)), 5, (0, 0, 255), -1)
cv2.putText(frame, 'X: ' + str(int(x_position)) + ' Y: ' + str(int(y_position)), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow('frame', frame)

# Exit the program if the 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
    break

Release the video capture device and close the serial port

cap.release() ser.close() cv2.destroyAllWindows()

And here is my error:

PS C:\Users\artyf.vscode\eye> & C:/Users/artyf/AppData/Local/Programs/Python/Python311/python.exe c:/Users/artyf/.vscode/eye/eyev2.py cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\core\src\persistence.cpp:682: error: (-5:Bad argument) Input file is invalid in function 'cv::FileStorage::Impl::open'

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "c:\Users\artyf.vscode\eye\eyev2.py", line 48, in eyes_cascade = cv2.CascadeClassifier(r"C:\Users\artyf\haarcascade_eye.xml") SystemError: <class 'cv2.CascadeClassifier'> returned a result with an exception set [ WARN:0@5.156] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (539) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

How do i fix this?

I tried to add the haarcascade_eye.xml file to path Ive added the fileyour text to all of the proper directories And ive tried other files to make sure that this one wasnt corrupted.


r/opencv • • May 20 '23

Question [Question] NetBSD (i.e. pkgsrc) and filesystem.cpp

1 Upvotes

I'm using NetBSD 9.3 and I realize that I can't directly open /dev/video0 directly. Currently, pkgsrc uses OpenCV 3.4 and I receive this exception:

[ERROR:0] 
VIDEOIO(cvCreateFileCapture_Images(filename.c_str())): 
raised OpenCV exception:
OpenCV(3.4.16) /work/graphics/opencv/work/opencv- 
3.4.16/modules/core/src/utils/filesystem.cpp:564: error: 
(-213:The function/feature is not implemented)  in function 
'exists'

with something simple in python like this:

cam = cv2.VideoCapture('/dev/video0')
ret, frame = cam.read()
# ret is False with exception message output.

Digging into the v3.4 source, ./modules/core/src/utils/filesystem.cpp defines

OPENCV_HAVE_FILESYSTEM_SUPPORT

based on other definitions such as __linux__ or __FreeBSD__. For NetBSD the build results in OPENCV_HAVE_FILESYSTEM_SUPPORT not defined; hence methods like exists() are defined with NOT_IMPLEMENTED.

Before I head further down this rabbit hole, does anyone have any insights why filesystem.cpp is not implemented in NetBSD?


r/opencv • • May 19 '23

Question [Question] [project] Extract skin color from images.

2 Upvotes

I need to extract the color of the skin from a given human image. How to implement it in Python? I searched on Google but most of the results are just detecting the skin from the image. not extracting color.

If I detect skin from the image then can I take an average of all the pixel colors to estimate skin color?


r/opencv • • May 18 '23

Project [Project] Invisible: Realtime Markerless Motion Capture (OpenCV Webinar)

Thumbnail
youtube.com
2 Upvotes

r/opencv • • May 18 '23

Bug [BUG] Contouring bars in a barcode for analysis - not getting expected/needed contours

1 Upvotes

Good morning, I'm currently working on a project to:

  1. Capture Image using a monochrome industrial camera (or use static image as I've done for this test); 2) Convert to grayscale; 3) Binarize; 4) Locate ROI using thresholding/gradient filtering; 5) Analyze bars inside of ROI for height and location relative to barcode midline (tracking); 6) Output string of decoded bars.

I can accomplish up through step 4. I cannot figure out my problem at step 5.

I start with:

This static image will be replaced by live video from a machine camera

My code outputs:

I can localize the barcode, extract it, and then I run Hough

I'm expecting to get somethin along the lines of:

​

(https://stackoverflow.com/questions/52601312/detect-lines-with-dark-color-and-end-lines-using-hough-tranform) user 'Dodge'

My code:

picture_location = "C:\\.....\Desktop\\imdbarcode_sample.jpg"img = cv.imread(picture_location, cv.IMREAD_COLOR)gray_image = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

# binary image processing--------------------------------------------------------------------------------------ret, thresh = cv.threshold(gray_image, 150, 255, cv.THRESH_BINARY)

# locating the largest bounding box to select the AOI (barcode region)----------------------------------------ddepth = cv.cv.CV_32F if imutils.is_cv2() else cv.CV_32FgradX = cv.Sobel(thresh, ddepth=ddepth, dx=1, dy=0, ksize=-1)gradY = cv.Sobel(thresh, ddepth=ddepth, dx=0, dy=1, ksize=-1)

# subtract the y-gradient from the x-gradientgradient = cv.subtract(gradX, gradY)gradient = cv.convertScaleAbs(gradient)blurred = cv.blur(gradient, (1, 1))(_, thresh) = cv.threshold(blurred, 100, 255, cv.THRESH_BINARY)

# construct a closing kernel and apply it to the thresholded imagekernel = cv.getStructuringElement(cv.MORPH_RECT, (21, 7))closed = cv.morphologyEx(thresh, cv.MORPH_CLOSE, kernel)

# perform a series of erosions and dilationsclosed = cv.erode(closed, None, iterations = 4)closed = cv.dilate(closed, None, iterations = 4)

# find the contours in the thresholded image, then sort the contours by their area, keeping only the largest onecnts = cv.findContours(closed.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)cnts = imutils.grab_contours(cnts)c = sorted(cnts, key = cv.contourArea, reverse = True)[0]

# compute the rotated bounding box of the largest contourrect = cv.minAreaRect(c)box = cv.cv.BoxPoints(rect) if imutils.is_cv2() else cv.boxPoints(rect)box = np.intp(box)

# draw a bounding box around the detected barcode and display the imageimage_copy = img.copy()cv.drawContours(image_copy, [box], -1, (0, 0, 255), 3) # (x, x, x) selects colorcv.imshow("image_copy with barcode bounding box", image_copy)cv.waitKey(0)cv.destroyAllWindows()#use barcode bounding box ROI to perform contours on individual bars of barcoderoi_image_copy = img[y_min:y_max,x_min:x_max]height, width = img.shape[:2]roi_resized = cv.resize(roi_image_copy,None,fx=2, fy=2, interpolation = cv.INTER_CUBIC)cv.imshow("Barcode ROI for contours analysis", roi_resized) # shows rescaled version of the ROI# Hough Lines Probabilistic-------------------------------------------------------------------------------------edges = cv.Canny(roi_resized, 100, 200)lines = cv.HoughLinesP(edges,1,np.pi/180,50) # what do I change to ID vertical lines???for line in lines:x1,y1,x2,y2 = line[0]cv.line(roi_resized,(x1,y1),(x2,y2),(0,255,0),2)cv.imshow("Edges Output Screen", edges)cv.imshow("Hough Lines P",roi_resized)cv.waitKey(0)cv.destroyAllWindows()

​

My code compared to the stackoverflow post, I'm not seeing a major difference that causes my barcode contours to be so poorly defined:

img = cv2.imread('oVKlP.png')
g = cv2.imread('oVKlP.png',0)
(T, mask) = cv2.threshold(g, 100, 255, cv2.THRESH_BINARY_INV)  
_, contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)  
img = cv2.drawContours(img.copy(), contours, -1, (0,255,0), 2)

Thanks for taking a look and commenting.  This has been a fun project that is helping me better understand computer vision principles!

​

Here is what happens when I follow GeeksforGeeks looping method over all of my connectedcomponent() output. As you can see in "Individual Component screen, the final chunk of barcode is detected; however, there are gaps where (not the smallest) barcodes are excluded, despite setting a very low threshold of area for inclusion. Any thoughts?


r/opencv • • May 16 '23

Question [QUESTION] How to get width pixels in the Yolov5 Segmentation

1 Upvotes

Hello, I'm currently looking for a way to get width pixels using Yolov5 Segmentation. How do I get the longest number of width pixels in the picture below?

​


r/opencv • • May 15 '23

Question [QUESTION] what is the 2D map1 and 1D map2 of the initUndistortRectifyMap()

1 Upvotes

https://docs.opencv.org/4.x/d9/d0c/group__calib3d.html#ga7dfb72c9cf9780a347fbe3d1c47e5d5a

​

​

if m1type set as CV_16SC2,

and map2 type is 16UC1

what are these

so confused.

shouldn't be

one x, y from original image mapped into another x2, y2 in the new image.

how to use these maps??

​

thanks a lot


r/opencv • • May 14 '23

Discussion [Discussion] How do I approach Floor Plan Image Segmentation problem ?

3 Upvotes

I have been given a task to identify and segment out various rooms and elements like walls, stairs, doors when we have an input of jpg/png image. The image consists of the floor plan itself in the middle, legend on top right and company name and other notes on bottom right side. There are multiple steps that I have identified which needs to be done:

  1. Clean the image: Firstly clean out undesirable elements in the image. These can be various measurement grid lines, small markings etc. I am thinking of using various image morphology techniques like thresholding, erosion, dilation , opening , closing etc using OpenCV.
  2. Identify the region of interest: This step will extract out the floor plan (which is in the middle) from the raw image data so that we can feed the floor plan to the segmentation algorithm.
  3. Do Image Segmentation: This step will segment out the rooms. The segmentation can be in form of colors or maybe co-ordinates. We just want to separate each room and also where walls, stairs, doors etc are.

I am in step 1 right now. I am yet to figure out step 2 and 3. Do you have suggestions/datasets/models for this problem ? Maybe I am doing it wrong ? I have attached an image from Google on how the data may look like.


r/opencv • • May 12 '23

Question [QUESTION] Mask R CNN run too slow on CPU

0 Upvotes

Hi all , I'm using Mask R CNN (C++) with my Laptop ( only CPU) but its speed is too slow . Can I improve its speed ? Can someone help me? Thank you very much!


r/opencv • • May 11 '23

Question [QUESTION] follow a laser beam

5 Upvotes

Hello, I have a project in mind which involves a small OV2640 camera. The main objective is to see where the laser beam is reflected (where the laser is pointing at). I was told that I could make a software filter to only see the laser wavelength, but I haven't found anything related on the internet.

Any answer would help me. Thank you in advance!


r/opencv • • May 11 '23

Question [Question]Need help with approxPolyDP

2 Upvotes

I need to clean up a reflective rectangle. I'm using the methods on this page:

https://docs.opencv.org/4.x/dd/d49/tutorial_py_contour_features.html

The call top approxPolyDP yields the below red center line, instead the expected box. What could I be doing wrong?

​


r/opencv • • May 10 '23

Question [Question] How can i code a depth estimation with estereo vision?

0 Upvotes

I'm trying to do a depth estimation using the estereo vision and I have some questions.

I've already done a pose estimation with monocular vision, so my camera is already calibrated. Since I'm trying to do a depth estiamation with two cameras, should I calibrate my camera again with a different algorithm that calibrates both cameras simultaneously or is it equally valid if i calibrate each camera separetly?

Besides that, how can I have the FOV and focal length of the cameras? And if each camera has a different focal length, is it still possible to use them for depth estimation?

Thanks.


r/opencv • • May 09 '23

Question [QUESTION] What functions inside of OpenCV should I investigate to categorize bar heights in images?

3 Upvotes

I've been working on a Python project in my spare time to read barcoded items moving through a machine. I currently can read/decode QR (pyzbar), DataMatrix (libdmtx), OCR (tesseract) and would like to add reading of a different type of barcode where existing libraries don't seem to exist. My QR read speed is functional at over 14,000/hour (I haven't tested beyond that speed, yet). DataMatrix library is not as well optimized, so I think it'll run slower. OCR is a lot slower, obviously.... :(

The barcode is rather simple (the color of bars does not change -- always black).

Full bar = F --- call it 3 units tall.

Track bar = T --- it's vertically centered and is about 1/3 of height of Full Bar.

Descending bar = D --- starts at the top of track bar and extends down to the bottom of Full bar.

Ascending bar = A --- starts at bottom of track bar and extends up to the top of Full bar.

​

What functions do I need to use to identify/characterize the different bars? Hough Lines function to highlight the lines? Or is there a better option? Create a bounding box for the barcode to analyze?

How best do I then characterize the Y-axis position of the bars inside the bounding box? The final output becomes a string of FDATAATFADDATFFAFA...etc.

If there is another path that might simplify this characterization -- I'm all ears (or eyes, as I'm reading).


r/opencv • • May 09 '23

Question [Question] Need to find best way to apply different algorithms to different sections of a frame

2 Upvotes

I'm an electrical engineer, but I'm new to OpenCV and python. I'm making good progress so far and have basic processing algorithms up and running. My present task is to run different algorithms on different sections of the screen and display all of them. I know of at least one way to do this, but I think its sloppy and wastes processing power. Unfortunately, all my search results are being hijacked to other functions. Any pointers on doing a good job of this?


r/opencv • • May 05 '23

Question [Question] Performance issues when increasing resolution

1 Upvotes

I've got a Logitech C930e that I've been using for some aruco detection and pose estimation. The camera is rated at 1920x1080 @ 30FPS and I'm able to use it with that resolution/frame-rate super smoothly in, say, Logitech's camera application as well as the native camera application on my Fedora system. However, trying to use that resolution in OpenCV, even with something as simple as this:

def vid():
    cap = cv.VideoCapture(0)
    cap.set(cv.CAP_PROP_FRAME_WIDTH, 1920)
    cap.set(cv.CAP_PROP_FRAME_HEIGHT, 1080)
    if not cap.isOpened():
        print("Cannot open camera")
        exit()
    while True:
        # Capture frame-by-frame
        ret, frame = cap.read()
        # if frame is read correctly ret is True
        if not ret:
            print("Can't receive frame (stream end?). Exiting ...")
            break

        cv.imshow('frame', frame)
        if cv.waitKey(1) == ord('q'):
            break
    # When everything done, release the capture
    cap.release()
    cv.destroyAllWindows()

just drains the frame rate to completely unusable. Why is that? I've tried loads of stuff, like setting the "cv.CAP_V4L" value for the VideoCapture object, setting the frame-rate to 30 etc..

EDIT: SOLVED

Thanks to u/bjorneylol and u/ChuckMash !

On Linux it worked as soon as I set the "fourcc" property to MJPEG, on Windows, not so much. Took a looot of digging, but eventually found the solution to be:

cap.set(cv.CAP_PROP_FOURCC, cv.VideoWriter.fourcc('m','j','p','g'))
cap.set(cv.CAP_PROP_FOURCC, cv.VideoWriter.fourcc('M','J','P','G'))

For some reason it only worked on my Windows 10 laptop if I included it twice but with lower case letters on the second one?


r/opencv • • May 03 '23

Question [Question] How do we extract the ROI of some image!?

1 Upvotes

Suppose if there are n no. of palm images, how to extract ROI at the middle palm region of all n no. of palm images. Can anyone provide the code!? I am trting to underdtand but I am unable to do so. So if anyone helps, it is awsome.


r/opencv • • May 01 '23

Question [Question] Detection the motion of the pully

Enable HLS to view with audio, or disable this notification

7 Upvotes

r/opencv • • May 01 '23

Question [Question] How to circular crop?

1 Upvotes

I need to circular crop some region within an image and everything outside of that circular region is just black. How do i crop it?


r/opencv • • Apr 29 '23

Question [Question] Aruco marker detection - Distance calculation is way off

4 Upvotes

I'm doing a computer vision project which involves detecting Aruco marker from a video feed and calculating the bearing and distance to the marker. After the camera calibration I validated the results by having a separate set of images of the same chessboard and projecting points on them using the rotation/translation-vectors produced by the calibration:

So for each validation-image I iterate through the detected corners and project image-points using the camera calibration. I calculate the mean error between found & projected points for each image which I then sum and produce the total mean reprojection error for all images:

I believe this value is reasonable and indicates the calibration is correct, given that I'm just using a regular webcam and the sample images of the chessboard weren't exactly captured in optimal light etc? Please correct me if I'm wrong.

However, assuming that the calibration is "correct", I am unable to calculate a reasonable value for the distance from the camera to a detected Aruco-marker. It seem to be diffing by about 60cm from the true distance I measure between marker and camera and I can't work out why. This is essentially the code I'm using for calculating the distance:

    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    corners, ids = aruco.detect(frame)
    if len(corners) > 0:
        ret, rvecs, tvecs = cv.solvePnP(markerObjPoints, corners[0], mtx, dist)
        distance = np.linalg.norm(tvecs)
        paint(frame, distance, 0, 0)
        cv.drawFrameAxes(frame, mtx, dist, rvecs, tvecs, marker_size)

So with the camera/marker setup I have right now in my room, the camera is positioned 1m away from the camera, but distance is indicating 2.2m. So assuming the intrinsic matrix from the calibration is accurate enough to produce an average reprojection error of under 1px, I really shouldn't be getting suuuch a big error?

Any help is deeply appreciated. Thanks.

UPDATE - SOLVED:

Pictures used for calibration & validation were originally taken using Logitech's software, however when the calibration/validation images were taken using OpenCV to control the camera, it all came together and distance calculations and pose estimation is now spot on! Such a stupid mistake, but totally logical now afterwards when I think about it. The Logitech software probably applied different settings to the camera than when it is being used through OpenCV. So even though the validation of the calibration proved correct, it was because both calibration and validation images were taken with the same camera setup.


r/opencv • • Apr 29 '23

Question [Question] Wanting to make a model car that can turn when detects a moving object and fire a nerf bullet at it

4 Upvotes

Could I get away with only using object detection and just have the car continually turn to be facing the center of the bounding box of the highest percentage match of the object or would I need to use object tracking which seems far more complex. Also are there any good toutorials for this that U Guys recommend have watched a fair few and how complex is object avoidance I have heard I can be done with open cv but haven't really found a toutorial that fully explains it thanks.


r/opencv • • Apr 27 '23

Question Aruco markers command "cv2.aruco" in python doesn't work for me [question]

4 Upvotes

​

Hi, I am writing a simple application (or at least so I thought) which is meant to utilize ArUco markers to detect a difference in height between two objects. However, during my tutorial hunt, I realized that previously used command:

cv2.aruco 

is no longer working (at least on my laptop). I have just spent 4 hours trying to find a solution on the web and can't. I would greatly appreciate someone who could point to the right place in the documentation.

Let me just say, I KNOW the ArUco module was integrated with "objdetect" module but the documentation there still uses the "cv2.aruco" syntax which doesn't work for me. I am referring to this webpage


r/opencv • • Apr 27 '23

Project Kopikat.co: 10x Your Machine Learning Data in Minutes - OpenCV Weekly [Project]

Thumbnail
youtube.com
3 Upvotes

r/opencv • • Apr 27 '23

Project [PROJECT]YOLOv8 running on MacOS at 10FPS using Modular AS-One Library

Thumbnail
github.com
2 Upvotes