Opencv Pythonprogram - python

I am trying opencv with python now.
What mean this error?
OpenCV Error: Assertion failed (m.dims >= 2) in Mat, file /build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/core/src/matrix.cpp, line 269
Traceback (most recent call last):
File "sabun5.py", line 16, in <module>
img_m = cv2.threshold(img_df, 50, 255, cv2.THRESH_BINARY)[1]
cv2.error: /build/opencv-ISmtkH/opencv-2.4.9.1+dfsg/modules/core/src/matrix.cpp:269: error: (-215) m.dims >= 2 in function Mat

You can see in the OpenCV documentation, that the threshold function just allow single-channel images.
If your image is a color one, it won't work. If it's grayscale but you are loading it with imread, it might be possible that OpenCV load it as a 3-channel one. You can add the flag to load it as a single-channel with CV_8UC1 (supposing it is an 8 bit unsigned one, which is the more common for a grayscale image).
For example:
img_df = cv2.imread("image/path", cv2.CV_8UC1)

you have to convert the image into grayscale before thresholding your image has more then two dimentions i.e (height,width,color-channel) gray scale image has only two dimention(height,width)
it might help
import cv2
img = cv.imread('x.png',0)
# where 0 converts the image in grayscale or gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_m = cv2.threshold(img, 50, 255, cv2.THRESH_BINARY)[1]
cv2.waitKey(0)

Related

how to binaries the image, assign the zeros and one into 2D image by a threshold point using python and convert it to RGB?

I have written the following code in MATLAB and want to convert it into python code. How can we find the MatLab instruction into python:
MatLab Code:
path = 'C:\Users\hp\Desktop\output\result_0.png';
img = imread(path);
size(img)
img = rgb2gray(img);
size(img)
m = mean(img(:));
img = img>m;
RGB = cat(3, img, img, img);
size(RGB)
Code in Python:
path = r"C:\\Users\\hp\\Desktop\\output\\result_0.png"
img = cv2.imread(path)
print(img.shape)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
result = gray.flatten()
m = statistics.mean(result)
print(m)
gray = gray>m
img = gray.astype(int)
print(img)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
how to correct the python code, so that it works as MatLab code?
I am getting the error after running the python code:
Traceback (most recent call last):
File "C:/pytorch-GaitGAN-master/src/test2.py", line 24, in <module>
cv2.imshow('image',img)
cv2.error: OpenCV(4.0.0) c:\projects\opencv-python\opencv\modules\imgproc\src\color.hpp:261: error: (-2:Unspecified error) in function '__cdecl cv::CvtHelper<struct cv::Set<1,-1,-1>,struct cv::Set<3,4,-1>,struct cv::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)'
> Unsupported depth of input image:
> 'VDepth::contains(depth)'
> where
> 'depth' is 4 (CV_32S)
the following code, I was required:
import cv2
import numpy as np
path = r"C:\\Users\\hp\\Desktop\\output\\result_0.png"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray[gray<np.mean(gray)] = 0.
gray[gray>=np.mean(gray)] = 255.
print(gray.shape)
img = np.stack((gray,)*3, axis=-1)
print(img.shape)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Based on your Matlab code, I think you want to threshold you image based on the mean value m. You can use cv2.threshold to do this.
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
result = gray.flatten()
m = statistics.mean(result)
gray_thres = cv2.threshold(gray,m,255,cv2.THRESH_BINARY)
You may need to change the value 255 to the max value in your image. More info here.

Getting error when trying to mask grayscale image based on how dark/light it is

The code I am trying to write should make the image feed grayscale, get the pixels that are quite dark, create a mask from those values and then use that mask to crop out the background.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
#get's the cameras video
_, frame = cap.read()
#Converts to grayscale
grayscale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#black values
white = ([255,255,255])
gray= ([100,100,100])
#Finds dark pixels
mask = cv2.inRange(grayscale,gray,white)
res = cv2.bitwise_and(frame,frame,mask=mask)
cv2.imshow('frame'.frame)
cv2.imshow('mask',mask)
cv2.imshow('res',res)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
and when I run the code I get this error mesage:
Traceback (most recent call last):
File "C:/Users/molta/AppData/Local/Programs/Python/Python37/webcam.py", line 17, in <module>
mask = cv2.inRange(grayscale,gray,white)
TypeError: Expected cv::UMat for argument 'lowerb'
I've tried searching for an answer, both on stackoverflow and google, but found nothing similar. I've also tried to understand what cv::UMat is, but sadly the documenation is poor to say the least.
from #furas comment, since your grayscale is a 2d array, only 1 value for white and gray is valid.
white = np.array(255)
gray = np.array(100)
It has to be numpy.array
white = np.array([255,255,255])
gray = np.array([100,100,100])
and then cv2.inRange(grayscale, gray, white) works
cv2.inRange takes scalar bounds for a grayscaled image having size M x N with one channel in opencv. What you are doing above is for an image of size M x N with 3 channels.
So, Replace
white = ([255,255,255])
gray= ([100,100,100])
#Finds dark pixels
mask = cv2.inRange(grayscale, gray, white)
with
white = 255
gray = 100
#Finds dark pixels
mask = cv2.inRange(grayscale, gray, white)
Also, correct line cv2.imshow('frame'.frame) to cv2.imshow('frame', frame).

OpenCV: Error with adaptive thresholding (Error -215)

I am trying to do adaptive thresholding after applying Sobel filtering on an image, as seen in the code below:
import numpy as np
import matplotlib.pyplot as plt
import cv2
image = cv2.imread("train.jpg")
img = np.array(image, dtype=np.uint8)
#convert to greyscale
img_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#remove noise
img_smooth = cv2.GaussianBlur(img_grey, (13,13), 0)
sobely = cv2.Sobel(img_smooth,cv2.CV_64F,0,1,ksize=9)
thres = cv2.adaptiveThreshold(sobely, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 73, 2)
However, when I try to do adaptive thresholding, I get the following error:
cv2.error: OpenCV(4.1.0) /Users/travis/build/skvark/opencv-
python/opencv/modules/imgproc/src/thresh.cpp:1627: error: (-215:Assertion
failed) src.type() == CV_8UC1 in function 'adaptiveThreshold'
I have read that this error occurs if your image is not greyscale, however, I converted my image to greyscale at the start of the code. I am not sure why I am getting this error. Any insights are appreciated.
What you read is correct, that error actually means that your Mat is not a gray-scale image.
That happens because Sobel is changing the data to cv2.CV_64F (see the second parameter in the documentation here).
So after Sobel you need to convert the image to a gray-scale, you can do that with convertScaleAbs and after that pass it's output to adaptiveThreshold

Why i have this issue with cv2.findContours function?

Traceback (most recent call last):
File "C:/Users/michail.gakas/Desktop/python scripts/counters.1py.py", line 10, in
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
error: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\color.cpp:7456: error: (-215) scn == 3 || scn == 4 in function cv::ipp_cvtColor
My code:
import numpy as np
import cv2
img = cv2.imread('star.jpg',0)
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
I am using python 2.7 CV3 but i had CV2 installed before
I don't have OpenCV with me right now but from what I see you did
img = cv2.imread("star.jpg", 0)
but what you probably wanted to do is open it in color as:
img = cv2.imread("star.jpg", 1)
or open it "unchanged" as:
img = cv2.imread("star.jpg", -1)
What you did is you opened an image in grayscale mode and then tried to convert it to grayscale. That error actually states that assert didn't find an image with 3 or 4 channels and BGR2GRAYSCALE goes from a color jpg image (3 channels) or a color png image (4 channels, 1 for alpha sometimes) to a 1 channel grayscale image. Alpha channel is discarded in this function. Pls make your life easier and use the official flags cv2 offers for easier code readability.
cv2.IMREAD_UNCHANGED (<0) loads the image as is (including the alpha channel if present)
cv2.IMREAD_GRAYSCALE ( 0) loads the image as an intensity one
cv2.IMREAD_COLOR (>0) loads the image in the RGB format

OpenCV findContours in python

I am working in python on openCV 3.0. In order to find the largest white pixel region, first of all thresholded gray image to binary image.
import cv2
import numpy as np
img = cv2.imread('graimage.png')
img = cv2.resize(img,(400,500))
gray = img.copy()
(thresh, im_bw) = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY )
derp,contours,hierarchy = cv2.findContours(im_bw,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
cnts = max(cnts, key=cv2.contourArea)
But it shows error as follows.
cv2.error: ..../opencv/modules/imgproc/src/contours.cpp:198: error: (-210) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function cvStartFindContours.
It looks like this was answered in the comments, but just to mark the question as answered:
CV_8UC1 means 8-bit pixels, unsigned, and only one channel, so grayscale. It looks like you're reading it in with 3 color channels, or CV_8UC3. You can check the image type by printing img.dtype and img.shape. The dtype should be uint8, and the shape should be (#, #), indicating two dimensions. I'm guessing you'll see that shape prints (#, #, 3) for your image as-is, indicating three color channels.
As #user3515225 said, you can fix that by reading the image in as grayscale using cv2.imread('img.png', cv2.IMREAD_GRAYSCALE). That assumes you have no use for color anywhere else, though. If you want a separate grayscale copy of the image, then replace gray = img.copy() with gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) instead.

Categories

Resources