Posts

Showing posts with the label Opencv

Choosing The Correct Upper And Lower HSV Boundaries For Color Detection With`cv::inRange` (OpenCV)

Image
Answer : Problem 1 : Different applications use different scales for HSV. For example gimp uses H = 0-360, S = 0-100 and V = 0-100 . But OpenCV uses H: 0-179, S: 0-255, V: 0-255 . Here i got a hue value of 22 in gimp. So I took half of it, 11, and defined range for that. ie (5,50,50) - (15,255,255) . Problem 2: And also, OpenCV uses BGR format, not RGB. So change your code which converts RGB to HSV as follows: cv.CvtColor(frame, frameHSV, cv.CV_BGR2HSV) Now run it. I got an output as follows: Hope that is what you wanted. There are some false detections, but they are small, so you can choose biggest contour which is your lid. EDIT: As Karl Philip told in his comment, it would be good to add new code. But there is change of only a single line. So, I would like to add the same code implemented in new cv2 module, so users can compare the easiness and flexibility of new cv2 module. import cv2 import numpy as np img = cv2.imread('sof.jpg') ORANGE_MIN = np....

Apply MatplotLib Or Custom Colormap To OpenCV Image

Image
Answer : For Python >= 2.7, cmapy packages this functionality in a convenient way. Install it with: Python 2.7: pip install cmapy Python 3.x: pip3 install cmapy Or, for Anaconda (from conda-forge): conda install -c conda-forge cmapy And use it like this: import cv2 import matplotlib.pyplot as plt import cmapy # Read image. img = cv2.imread('imgs/woman.png') # Colorize. img_colorized = cv2.applyColorMap(img, cmapy.cmap('viridis')) # Display plt.imshow(img_colorized) plt.show() Different colormaps give something like this: See all the available colormaps in action here. Disclaimer: I wrote cmapy (because I needed this functionality for another project), and internally, it does pretty much the same as the other answers. In recent versions of OpenCV (starting with 3.3), there's an overload of applyColorMap , which allows you to provide a custom colormap (either 1 or 3 channel). I've modified verified.human's code to sim...

Alternative Segmentation Techniques Other Than Watershed For Soil Particles In Images

Image
Answer : You could try using Connected Components with Stats already implemented as cv2.connectedComponentsWithStats to perform component labeling. Using your binary image as input, here's the false-color image: The centroid of each object can be found in centroid parameter and other information such as area can be found in the status variable returned from cv2.connectedComponentsWithStats . Here's the image labeled with the area of each polygon. You could filter using a minimum threshold area to only keep larger polygons Code import cv2 import numpy as np # Load image, Gaussian blur, grayscale, Otsu's threshold image = cv2.imread('2.jpg') blur = cv2.GaussianBlur(image, (3,3), 0) gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] # Perform connected component labeling n_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(thresh, connectivity=4) # Create fal...

Camera Position In World Coordinate From Cv::solvePnP

Answer : If with "world coordinates" you mean "object coordinates", you have to get the inverse transformation of the result given by the pnp algorithm. There is a trick to invert transformation matrices that allows you to save the inversion operation, which is usually expensive, and that explains the code in Python. Given a transformation [R|t] , we have that inv([R|t]) = [R'|-R'*t] , where R' is the transpose of R . So, you can code (not tested): cv::Mat rvec, tvec; solvePnP(..., rvec, tvec, ...); // rvec is 3x1, tvec is 3x1 cv::Mat R; cv::Rodrigues(rvec, R); // R is 3x3 R = R.t(); // rotation of inverse tvec = -R * tvec; // translation of inverse cv::Mat T = cv::Mat::eye(4, 4, R.type()); // T is 4x4 T( cv::Range(0,3), cv::Range(0,3) ) = R * 1; // copies R into T T( cv::Range(0,3), cv::Range(3,4) ) = tvec * 1; // copies tvec into T // T is a 4x4 matrix with the pose of the camera in the object frame Update: Later, to use T with OpenGL ...

Anaconda: Cannot Import Cv2 Even Though Opencv Is Installed (how To Install Opencv3 For Python3)

Answer : opencv is not compatible with python 3. I had to install opencv3 for python 3. The marked answer in how could we install opencv on anaconda? explains how to install opencv(3) for anaconda: Run the following command: conda install -c https://conda.binstar.org/menpo opencv I realized that opencv3 is also available now, run the following command: conda install -c https://conda.binstar.org/menpo opencv3 Edit on Aug 18, 2016: You may like to add the "menpo" channel permanently by: conda config --add channels menpo And then opencv can be installed by: conda install opencv (or opencv3) Edit on Aug 14, 2017: " clinicalgraphics " channel provides relatively newer vtk version for very recent python3 conda install -c clinicalgraphics vtk Edit on April 16, 2020 (based on @AMC's comment): OpenCV can be installed through conda-forge (details see here) conda install -c conda-forge opencv You can try conda install -c menpo opencv=3 Use this cod...

Access IP Camera In Python OpenCV

Image
Answer : An IP camera can be accessed in opencv by providing the streaming URL of the camera in the constructor of cv2.VideoCapture . Usually, RTSP or HTTP protocol is used by the camera to stream video. An example of IP camera streaming URL is as follows: rtsp://192.168.1.64/1 It can be opened with OpenCV like this: capture = cv2.VideoCapture('rtsp://192.168.1.64/1') Most of the IP cameras have a username and password to access the video. In such case, the credentials have to be provided in the streaming URL as follows: capture = cv2.VideoCapture('rtsp://username:password@192.168.1.64/1') This works with my IP camera: import cv2 #print("Before URL") cap = cv2.VideoCapture('rtsp://admin:123456@192.168.1.216/H264?ch=1&subtype=0') #print("After URL") while True: #print('About to start the Read command') ret, frame = cap.read() #print('About to show frame of Video.') cv2.imshow("C...