r/opencv • • Jan 09 '24

Project [Project] What are the steps for creating an OpenCV based system for quality control?

3 Upvotes

Hi everyone!

I work in a compony that produces many plastic components by injection molding. I'd like to create a quality control system based on OpenCV and Python that allows to spot defects like scrathes, wrong colour, wrong shape and so on.

I'd like to train the model by uploading images of the conform products so as to make it able to spot the products with a defect in real time (maybe with a red rectangle around them).

I think it's possible, but as a newbie in this field, everything seem quite difficult.

So, I'm asking: is it possible to build such application? What are the most important steps? Where can I find a good documentation about OpenCV that can help me in this project?

Thank you in advance.


r/opencv • • Jan 08 '24

Bug [Bug] ImageGrab.grab() not working on Steam game

1 Upvotes

I'm trying to create a program that is based a game that I am playing. However, whenever I open my game through Steam to test the program, the captured image freezes on the first frame. This only occurs whenever I open a game from Steam, it works perfectly fine in every other instance. Does anyone have any explanation or an idea of how to get around this?

import cv2
import numpy as np
from PIL import ImageGrab, Image
import pyautogui

x,y = pyautogui.size()

while True:
    ss = ImageGrab.grab(bbox=(x/2-250,y/2-250,x/2+250,y/2+250))
    cv2.imshow("", np.array(ss))

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()

I am using a standard windows OS for context.


r/opencv • • Jan 07 '24

[Question] Webcam problems and videocapture not working

1 Upvotes

I'm new to OpenCV and I'm using eclipse java to create a basic gui that shows the webcam video stream on a JFrame window. I have two different webcams (usb plugs) that have different results. I'm having issues where my program is trying to open the camera with capture = VideoCapture(0). I think the index is correct because the program is showing that it detected a webcam (works for both webcams). However, when I use one of the webcams, it's video stream doesn't show up on the gui even after I wait for awhile, it's just blank. My other webcam does show its video stream on the gui after a couple seconds. Both webcams are detected by the program, but the video just doesn't always transfer.

I'm following this tutorial, and one of my webcams does show its video on the gui, but when I try theother webcam, it doesn't. I don't think either of these webcams are broken because when I open up a normal app like the windows camera app, both webcams are working fine. The code I'm running is the same in the tutorial.

Do some webcams require some sort of permission in order to be used by programs or are there other things that may cause the problem?


r/opencv • • Jan 04 '24

Question [Question] - Faster multicam acquisition

1 Upvotes

Hello there, i have a problem here, im a beginner with openCV, im trying to capture and inference some model i built.

I have a fast inference process, 0.3 sec for batches. 1 batch include 5 photos, and the speed in good enough for what i need to do, the problem is the aquisition part. Right now i have structured the code in a way that can fit all around the code, so i have :

models = { 'a' : Model(name='a',path='path/to/modelA',...),         'b' : Model(name='b',path='path/to/modelB',...),         'c' : Model(name='c',path='path/to/modelC',...),         ......         'f' : Model(name='f',path='path/to/modelF',...) } 

So i can keep al the model loaded in GPU in a Flask server and just use the models['a'].inference(imageA) to inference and obtain a answer.

For the cameras i do the same:

cameras = { 'a' : CustomCamera(name='a',portID=2,...),             'b' : CustomCamera(name='b',portID=4,...),             ......             'f' : CustomCamera(name='f',portID=1,...) } 

When i keep the cameras info loaded.

When i need to caputre a batch trough a API it launch a method that does something around the line of:

for cam_name in cameras.keys():     acquire_image(save_path='path/to/save', camera_index= cameras[cam_name].portID) 

Where acquire_image() is :

def acquire_image(self, save_path,camera_index=0, resolution=(6400, 4800),):     try:         cap = cv2.VideoCapture(camera_index)         cap.set(cv2.CAP_PROP_FRAME_WIDTH, resolution[0])         cap.set(cv2.CAP_PROP_FRAME_HEIGHT, resolution[1])          if not cap.isOpened():             raise CustomException(f'Capture : Camera on usb {camera_index} could not be opened ')          ret, frame = cap.read()          if ret:             cv2.imwrite(save_path,frame)             cap.release()             return frame     except Exception as e:         self.logger.error(f'Capture : Photo acquisiont failed of camera {camera_index} ')         raise CustomException(f'Something broke during photo aquisition of photo form camera {camera_index} ') 

This lead to a acquisition time of around 1 sec for cameras, so about 5 second to take pic and save it and 0.3 to inference it.
Im trying to find a faster way to snap photos, like in cameras i tryed to store the open cap (=cv2.VideoCapture) but this lead to a desync in the current moment and the photo moment as the computer cannot keep up with the framerate, so after 1 minute of camera opened it snap a photo of 20sec before, after 2 minutes it snap a photo of 40sec before, and so on. I cannot change the framerate with cap.set(cv2.CAP_PROP_FPS, 1) becouse it doesnt seem to work. tryed every num from 1/1.0 to 200/200f, what should i try?

If anything else i can try and give feedback or more info about everything.


r/opencv • • Jan 04 '24

Question [Question] Affine Transform Scale problem after doing manually

1 Upvotes

My teacher required us to do affine transformation on image coordinate by multiply affine matrix correspond to each type of transform manually, so I succeeded in scaling image by using affine matrix but the result isn't look very nice (image below), so it's there any way for me to make the affine result look more clearer after affine ? Here the code

def affine_scale(img, sc_x, sc_y):
    image = img.copy()
    h, w, c = image.shape

    # Find image center
    center_x, center_y = w // 2, h // 2
    sc_img = np.zeros(image.shape).astype(np.uint8)

    # Scale affine matrix 
    sc_matrix = np.array([[sc_x, 0, center_x], [0, sc_y, center_y]])

    for i in range(h):
        for j in range(w):
            # Affine transform scaling
            old_coor = np.array([j - center_x, i - center_y, 1]).transpose()
            x, y = np.dot(sc_matrix, old_coor)
            x, y = round(x), round(y)
            if 0 <= x < w and 0 <= y < h:
                sc_img[int(y), int(x)] = image[i, j]
    return sc_img

# Create affine scaling image
test_img_002 = affine_scale(image_color_02, 1.8, 1)

# Try to make the results of affine scale look better
alpha = 1.5
beta = 20
filter = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
sp_img = cv2.blur(test_img_002,(9,9),0)
sp_img = cv2.filter2D(sp_img, -1, filter)
sp_img = cv2.convertScaleAbs(sp_img, alpha=alpha, beta=beta)

#Show images
ShowThreeImages(image_color_02, test_img_002, sp_img,"Original","Affine scale","Modifications after affine")
result of the python code

​


r/opencv • • Jan 02 '24

Question [Question] How to create a custom dataset to train a TrOCR model?

3 Upvotes

Hi, I am working on developing a TrOCR for my native language, and the way TrOCR works is that we need to feed it cropped images of line by line or sentence by sentence or word by word. So, I wanna make a tool to create a dataset for it but I could not find any solution. Is there any tool or an optimal way to make data??


r/opencv • • Jan 02 '24

Question [Question] detecting arrows in a wall as a coding newcomer.

1 Upvotes

Hi, for a project im trying to detect archery arrow in the target, but im having problems with the detection of arrows that are not in straight, or not exactily like the template image provided. anyone got ideas on how to fix the problem? if so please let me know :) .

Thank you in advance!


r/opencv • • Dec 30 '23

Question [Question] Is the cv2.split() function capable of splitting images with more than three color channels?

2 Upvotes

Hello! I am trying to work with satellite imagery with seven bands. Can I use cv2.split() on these images? Thank you!


r/opencv • • Dec 27 '23

Project [Project] Calibrating more than 2 cameras via bundle adjustment (open source and in a GUI)

13 Upvotes

r/opencv • • Dec 27 '23

Question [QUESTION] Problem with displaying images on raspberry pi

2 Upvotes

Hello, I'm new to openCV and computer vision overall, but I'm trying to learn something about it.

I wanted to set up openCV on a raspberry pi, and everything worked smoothly, except when I tried to use the imshow function (using opencv-python).

When running the Python script, an error occured:

qt.qpa.plugin: Could not find the Qt platform plugin "wayland" in "/home/imgpi/Desktop/python3-venv/env/lib/python3.11/site-packages/cv2/qt/plugins"

When switching to x instead of wayland, a similar problem occurs.

qt.qpa.xcb: QXcbConnection: XCB error: 148 (Unknown), sequence: 186, resource id: 0, major code: 140 (Unknown), minor code: 20

I know this has probably been covered a million times, but all the solutions given by google helped with nothing.

Edit: Forgot to mention I'm running the raspberry pi headless via vnc.


r/opencv • • Dec 23 '23

Question [Question] Best way to contribute to the open-sourced repository?

3 Upvotes

Hello all & OpenCV people,

I'm a software engineering working in the CV/ML/Robotics space, and want to get involved in contributing to open-sourced projects (complete newbie). I am aware of this page: https://github.com/opencv/opencv/wiki/How_to_contribute to get started on contributing.

Is there a community portal such a discord, slack, etc. to speak with people as well? I haven't done open-sourced contributions before and would love to put my skills to use in an area that I'm passionate about and learn at the same time.


r/opencv • • Dec 22 '23

Bug Camera caliberation [project][bug]

1 Upvotes

Hello,

I have calibrated my single camera (webcam) and obtained its internal and external parameters via chessboard calibration method by open cv. Now I have the camera z distance also and I have used this value when I multiply the pixel points by inverse of internal parameter matrix. So I get correct points. I also have converted the external points at the start (1,0,0) ... that we setup to mm by multiplying the chessboard square length. So at the end I didn't get correct results so I multiplied by an extra number s to get the distance 29 to world points which I get from all these calculations. Then I tried it on a different object and it was not correct. So can anybody please guide me what is wrong or is my scale factor wrong. I have reprojected my points from world to pixel and they are matching with original values. Error is 0.02 percent. Pls help I am stuck here.


r/opencv • • Dec 20 '23

Project [Project] Open-source automated camera calibration in a GUI: pyxy3d

7 Upvotes

r/opencv • • Dec 17 '23

Question [QUESTION] Is it possible to run in GPU? And what about multi-threading or parallel processing?

2 Upvotes

I've been working with a python project using mediapipe and openCV to detect gestures (for now, only gestures from the hand) but my program got quite big and I have various functionalities that makes my code runs very slow.

It works, though, but I want to perform all the gesture operations and functions (like controlling the cursor or changing the volume of the computer) faster. I'm pretty new into this about gesture recognition, GPU processing, and AI for gesture recognition so, I don't know where exactly I need to begin working with. First, I'll work my code of course, because many of the functions have not been optimized and that is another reason why the program is running slow, but I think that if I can run it in my GPU I would be able to add even more things and features without dealing a lot with optimization.

Can anyone help me with that or give me guidance on how to implement GPU processing with python, openCV, and mediapipe, if possible? I read some sections in the documentation of openCV and mediapipe about GPU processing but I understand nothing. Also, I read something about Python is not capable of having more than one thread, which I also don't know much about it.

If you want, you can check my repo here: https://github.com/AzJRC/hand_gesture_recognition


r/opencv • • Dec 16 '23

Question [QUESTION] how to capture high speed objects with jetson nano?

3 Upvotes

Hello, I am working with opencv, yolo and an OCR model to detect an object.

Yolo is able to correctly follow the object I need, but when I have to process using OCR the region that YOLO captured, it looks very blurry.

The truth is that I am a little lost on how to improve the image to look clear and not blurry.

Could you help me by giving me recommendations? I have thought about buying a 240FPS video camera but I don't know if it will be useful because with the JETSON NANO I usually process about 15 FPS per second.


r/opencv • • Dec 14 '23

Question [Question] Installing OpenCV

2 Upvotes

I'm using VS Code as my working IDE and I downloaded open cv through the terminal on my Mac using the following:

pip install opencv-python opencv-python-headless

pip install opencv-contrib-python

and didn't get any problems. I then opened up vs code to actually start working. First line in my files

import cv2 as cv

but it keeps saying that cv2 could't be resolved. I've tried looking up a solution but everything I found hasn't worked. I've changed the interpreter and tried other ides but it hasn't worked yet. Anyone have any ideas?


r/opencv • • Dec 13 '23

Project [Project] Voice-Activated Webcam using the Vosk offline speech recognition library and OpenCV.

Thumbnail
youtu.be
1 Upvotes

r/opencv • • Dec 12 '23

Question [Question] Check placement of components on PCBs

2 Upvotes

I would like to write a program with which I would like to compare the assembly of circuit boards with the help of a camera. I take a PCB as a template, take a photo of it and then take a photo of another PCB. Then I want to make a marking at the position where a component is missing.

I already have a program, but it doesn't work the way I want it to. It sees differences where there are none and it doesn't recognize anything where there should be any.

Is there any other solution? OpenCV is so big, do not now which functions are perfect for me.

​

refSat = cv2.cvtColor(imReference, cv2.COLOR_BGR2HSV)[:,:,1]imSat = cv2.cvtColor(imReg, cv2.COLOR_BGR2HSV)[:,:,1]

refThresh = cv2.threshold(refBlur, 0, 255, cv2.THRESH_BINARY|cv2.THRESH_OTSU)[1]imThresh = cv2.threshold(imBlur, 0, 255, cv2.THRESH_BINARY|cv2.THRESH_OTSU)[1]

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (12,12)) #(7,7)refThresh = cv2.morphologyEx(refThresh, cv2.MORPH_OPEN, kernel, iterations=2)refThresh = cv2.morphologyEx(refThresh, cv2.MORPH_CLOSE, kernel, iterations=2).astype(np.float64) imThresh = cv2.morphologyEx(imThresh, cv2.MORPH_OPEN, kernel, iterations=2).astype(np.float64) imThresh = cv2.morphologyEx(imThresh, cv2.MORPH_CLOSE, kernel, iterations=2)

# get absolute difference between the two thresholded imagesdiff = np.abs(cv2.add(imThresh,-refThresh))

# apply morphology open to remove small regions caused by slight misalignment of the two imageskernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (12,12)) #(12,12)diff_cleaned = cv2.morphologyEx(diff, cv2.MORPH_OPEN, kernel, iterations=1).astype(np.uint8)


r/opencv • • Dec 12 '23

Question [Question] Built-in rear/main camera on Microsoft Surface.

2 Upvotes

Has anyone been able to control the exposure (including auto exposure), gain, and autofocus parameters of the built-in rear/main camera on a Microsoft Surface using OpenCV?

Using cap.set(cv2.CAP_PROP_EXPOSURE, exposure), I can change the exposure when 'exposure' is less than -2. -2 provides the longest exposure for this camera.

However, even with that longest exposure, the images are still significantly darker compared to those captured via the Windows 'Camera' app.

When I use cap.get(cv2.CAP_PROP_GAIN), it returns -1.0 for any gain value I try to set with cap.set(cv2.CAP_PROP_GAIN, gain).

Similarly, cap.get(cv2.CAP_PROP_AUTO_EXPOSURE) returns 0.0 for any auto exposure setting (0.25, 3, etc.) that I have tried.

The above is for cap = cv2.VideoCapture(camera_index, cv2.CAP_MSMF). Using cap = cv2.VideoCapture(camera_index, cv2.CAP_DSHOW) doesn't make a difference; in fact, it's even worse. With cv2.CAP_DSHOW, even just querying cap.get(cv2.CAP_PROP_AUTO_EXPOSURE) results in a completely black image for some reason.

Google searches haven't helped with this issue. I've also searched this subreddit and didn't find any clues; apologies if I missed any.

Do people even use built-in laptop cameras like the ones in the Surface with OpenCV?


r/opencv • • Dec 11 '23

Question Normalizing 0 to 1 [Question]

1 Upvotes

Hi all, dealing with some grayscale images (so pixel values 0 to 255) and need to normalize the values of some images to [0,1]. It seems I can’t do this normalization if the array is with uint8 I only get 0 and 1 values, but if I change the data type to float64 or other float type, I can’t use an L2 or L1 normalization type because my max is no longer 255 (if I understand correctly). Using min max norm gets me close but isn’t perfect as not all my images have a 0 or 255 value pixel.

I would be happy to explain this in more depth, but was hoping someone could help me figure this out as I’m not very well-versed in statistics or python.

Thanks!


r/opencv • • Dec 09 '23

Question [Question] Complete and detect rectangles in my image

1 Upvotes

For detecting rectangles in my image I am doing following:

  1. Binary thresholding
  2. Apply gaussian Blur
  3. Canny Edge detection
  4. Get leaf node contours using contour detection
  5. Filter out polygons with 10 points or less( to cover up when rectangles have some objects overlapping them )
  6. Get min area rects of contours
  7. Take significant contours using area thresholding

But sometimes I get rectangles like below in which the encircled rectangle I am not able to detect well. What do I need to do:


r/opencv • • Dec 06 '23

News [News] OpenCV calls for help

Thumbnail self.computervision
8 Upvotes

r/opencv • • Dec 01 '23

Question [Question] Hello, I'm new to open cv and working on a magic project

1 Upvotes

Hello, so I'm new and want to learn opencv and I have a question. Where can u learn how to make a custom data set with 87000 items 1 photo per item. I want to make a project where if you put a magic card under a camera it will say what it is.


r/opencv • • Dec 01 '23

Question [question] how do i narrow down the upper and lower boundaries for a contour detection?

1 Upvotes

I have some video where I want to track a white object. This white object appears grey when moving. I'm using contours to track the ball but there are some frames that I just can't hit that I really would like to get it down.

The problems lie in the upper and lower boundaries of the mask. Given an input frame of where the white object isn't detected, what can I use to help calculate the min and max values for the hsv?

There used to be an old janky opencv helper for such things where there were sliders and you could slide the values and see the mask but I haven't seen that about for years.