How to use opencv reduce using cv2 python API - python

I checked the documentation but its incomplete: there is no mention of what rtype parameter actually is.
I think it's a reduce type but I can't find any of variables like cv2.CV_REDUCE_SUM etc... I found this problem with many function that use different variable names. What's the best way to find proper names in cv2 API?

I found out that the appropriate variable can be found in the following package
cv2.cv
If you use CV_REDUCE_SUM operator on uint8 image you have to explicitly provide dtype parameter of bigger range to avoid overflowing (e.g.
slice = cv2.reduce(image, 1, cv2.cv.CV_REDUCE_SUM, dtype=numpy.int32)
If you use CV_REDUCE_AVG operation, result can't overflow that's why setting dtype is optional.

There are some omisions in the current new cv2 lib. Typically these are constants that did not get migrated to cv2 yet and are still in cv only. Here is some code to help you find them:
import cv2
import cv2.cv as cv
nms = [(n.lower(), n) for n in dir(cv)] # list of everything in the cv module
nms2 = [(n.lower(), n) for n in dir(cv2)] # list of everything in the cv2 module
search = 'window'
print "in cv2\n ",[m[1] for m in nms2 if m[0].find(search.lower())>-1]
print "in cv\n ",[m[1] for m in nms if m[0].find(search.lower())>-1]

If you're finding this while using Open CV 3.x or later, these constants have been renamed to cv2.REDUCE_SUM, cv2.REDUCE_AVG, cv2.REDUCE_MAX, and cv2.REDUCE_MIN.
An example of the working reduce function:
reducedArray = cv2.reduce(im, 0, cv2.REDUCE_MAX)
GitHub issue for documentation

Related

pydicom is not defined

I'm trying to learn how to use pydicom for reading and processing dicom images. I'm using Python 3.
import dicom
import numpy
ds = pydicom.read_file(lstFilesDCM[0])
print(ds.pixel_array)`
I get an error NameError: name 'pydicom' is not defined. If I change
ds = pydicom.read_file(lstFilesDCM[0])
to
ds = dicom.read_file(lstFilesDCM[0])
(using dicom.read_file instead), I get the following error:
NotImplementedError: Pixel Data is compressed in a format
pydicom does not yet handle. Cannot return array
I also verified that pydicom is properly installed and updated.
How do i fix this?
You are trying to call a class that you have not imported before:
Use:
import pydicom
import numpy
ds = pydicom.read_file(lstFilesDCM[0])
print(ds.pixel_array)
or
import dicom
ds = dicom.read_file("the_name_of_file.dcm")
Documentation: http://pydicom.readthedocs.io/en/stable/pydicom_user_guide.html
If you want to get your hands on the pixel data, I suggest to use the convert program from the ImageMagick suite. You can either call this program from Python using the subprocess module. (See this example, where I convert them to JPEG format), or you can use one of the Python bindings.
If you want to manipulate the images, using the bindings might be preferable. But note that not all the bindings have been converted to ImageMagick version 7.

OpenCV python canny Required argument 'threshold2' (pos 4) not found

I am trying to isolate text from an image with openCV before sending it to tesseract4 engine to maximize results.
I found this interesting post and I decided to copy the source and try by mysdelf
However I am getting issue with the first call to OpenCV
To reproduce:
Simply copy the code from the gist
launch command script.py /path/to/image.jpg
I am getting issue:
Required argument 'threshold2' (pos 4) not found
Do you maybe have an idea of what does it means.
I am a javascript, java and bash script developer but not python...
In a simple version:
import glob
import os
import random
import sys
import random
import math
import json
from collections import defaultdict
import cv2
from PIL import Image, ImageDraw
import numpy as np
from scipy.ndimage.filters import rank_filter
if __name__ == '__main__':
if len(sys.argv) == 2 and '*' in sys.argv[1]:
files = glob.glob(sys.argv[1])
random.shuffle(files)
else:
files = sys.argv[1:]
for path in files:
out_path = path.replace('.jpg', '.crop.png')
if os.path.exists(out_path): continue
orig_im = Image.open(path)
edges = cv2.Canny(np.asarray(orig_im), 100, 200)
Thanks in advance for your help
Edit: okay so this answer is apparently wrong, as I tried to send my own 16-bit int image into the function and couldn't reproduce the results.
Edit2: So I can reproduce the error with the following:
from PIL import Image
import numpy as np
import cv2
orig_im = Image.open('opencv-logo2.png')
threshold1 = 50
threshold2 = 150
edges = cv2.Canny(orig_im, 50, 100)
TypeError: Required argument 'threshold2' (pos 4) not found
So if the image was not cast to an array, i.e., the Image class was passed in, I get the error. The PIL Image class is a class with a lot of things other than the image data associated to it, so casting to a np.array is necessary to pass into functions. But if it was properly cast, everything runs swell for me.
In a chat with Dan MaĊĦek, my idea below is a bit incorrect. It is true that the newer Canny() method needs 16-bit images, but the bindings don't look into the actual numpy dtype to see what bit-depth it is to decide which function call to use. Plus, if you try to actually send a uint16 image in, you get a different error:
edges = cv2.Canny(np.array([[0, 1234], [1234, 2345]], dtype=np.uint16), 50, 100)
error: (-215) depth == CV_8U in function Canny
So the answer I originally gave (below) is not the total culprit. Perhaps you accidentally removed the np.array() casting of the orig_im and got that error, or, something else weird is going on.
Original (wrong) answer
In OpenCV 3.2.0, a new method for Canny() was introduced to allow users to specify their own gradient image. In the original implementation, Canny() would use the Sobel() operator for calculating the gradients, but now you could calculate say the Scharr() derivatives and pass those into Canny() instead. So that's pretty cool. But what does this have to do with your problem?
The Canny() method is overloaded. And it decides which function you want to use based on the arguments you send in. The original call for Canny() with the required arguments looks like
cv2.Canny(image, threshold1, threshold2)
but the new overloaded method looks like
cv2.Canny(grad_x, grad_y, threshold1, threshold2)
Now, there was a hint in your error message:
Required argument 'threshold2' (pos 4) not found
Which one of these calls had threshold2 in position 4? The newer method call! So why was that being called if you only passed three args? Note that you were getting the error if you used a PIL image, but not if you used a numpy image. So what else made it assume you were using the new call?
If you check the OpenCV 3.3.0 Canny() docs, you'll see that the original Canny() call requires an 8-bit input image for the first positional argument, whereas the new Canny() call requires a 16-bit x derivative of input image (CV_16SC1 or CV_16SC3) for the first positional argument.
Putting two and two together, PIL was giving you a 16-bit input image, so OpenCV thought you were trying to call the new method.
So the solution here, if you wanted to continue using PIL, is to convert your image to an 8-bit representation. Canny() needs a single-channel (i.e. grayscale) image to run, first off. So you'll need to make sure the image is single-channel first, and then scale it and change the numpy dtype. I believe PIL will read a grayscale image as single channel (OpenCV by default reads all images as three-channel unless you tell it otherwise).
If the image is 16-bit, then the conversion is easy with numpy:
img = (img/256).astype('uint8')
This assumes img is a numpy array, so you would need to cast the PIL image to ndarray first with np.array() or np.asarray().
And then you should be able to run Canny() with the original function call.
The issue was coming from an incompatibility between interfaces used and openCV version.
I was using openCV 3.3 so the correct way to call it is:
orig_im = cv2.imread(path)
edges = cv2.Canny(orig_im, 100, 200)

TypeError: Image data can not convert to float

I am trying to create a 16-bit image like so:
import skimage
import random
from random import randint
xrow=raw_input("Enter the number of rows to be present in image.=>")
row=int(xrow)
ycolumn=raw_input("Enter the number of columns to be present in image.=>")
column=int(ycolumn)
A={}
for x in xrange(1,row):
for y in xrange(1,column):
a=randint(0,65535)
A[x,y]=a
imshow(A)
But I get the error TypeError: Image data can not convert to float.
This question comes up first in the Google search for this type error, but does not have a general answer about the cause of the error. The poster's unique problem was the use of an inappropriate object type as the main argument for plt.imshow(). A more general answer is that plt.imshow() wants an array of floats and if you don't specify a float, numpy, pandas, or whatever else, might infer a different data type somewhere along the line. You can avoid this by specifying a float for the dtype argument is the constructor of the object.
See the Numpy documentation here.
See the Pandas documentation here
This happened for me when I was trying to plot an imagePath, instead of the image itself. The fix was to load the image, and plotting it.
The error occurred when I unknowingly tried plotting the image path instead of the image.
My code :
import cv2 as cv
from matplotlib import pyplot as plt
import pytesseract
from resizeimage import resizeimage
img = cv.imread("D:\TemplateMatch\\fitting.png") ------>"THIS IS THE WRONG USAGE"
#cv.rectangle(img,(29,2496),(604,2992),(255,0,0),5)
plt.imshow(img)
Correction:
img = cv.imread("fitting.png") --->THIS IS THE RIGHT USAGE"
First read the image as an array
image = plt.imread(//image_path)
plt.imshow(image)
I was also getting this error, and the answers given above says that we should upload them first and then use their name instead of a path - but for Kaggle dataset, this is not possible.
Hence the solution I figure out is by reading the the individual image in a loop in mpimg format. Here we can use the path and not just the image name.
I hope it will help you guys.
import matplotlib.image as mpimg
for img in os.listdir("/content/train"):
image = mpimg.imread(path)
plt.imshow(image)
plt.show()
From what I understand of the scikit-image docs (http://scikit-image.org/docs/dev/index.html), imshow() takes a ndarray as an argument, and not a dictionary:
http://scikit-image.org/docs/dev/api/skimage.io.html?highlight=imshow#skimage.io.imshow
Maybe if you post the whole stack trace, we could see that the TypeError comes somewhere deep from imshow().
try
import skimage
import random
from random import randint
import numpy as np
import matplotlib.pyplot as plt
xrow = raw_input("Enter the number of rows to be present in image.=>")
row = int(xrow)
ycolumn = raw_input("Enter the number of columns to be present in image.=>")
column = int(ycolumn)
A = np.zeros((row,column))
for x in xrange(1, row):
for y in xrange(1, column):
a = randint(0, 65535)
A[x, y] = a
plt.imshow(A)
plt.show()
Try to use this,
plt.imshow(numpy.real(A))
plt.show()
instead of plt.imshow(A)
This happened because you may transfer a wrong type to imshow(), for example I use albumentations.Compose to change image, and the result is a dict rather than numpy.ndarray. so just change
plt.imshow(cv2.cvtColor(aug(image=img), cv2.COLOR_BGR2RGB))
to
plt.imshow(cv2.cvtColor(aug(image=img)['image'], cv2.COLOR_BGR2RGB))
then it works.
I guess you may have this problem in Pycharm. If so, you may try this to your problem.
Go to File-Setting-Tools-Python Scientificin Pycharm and remove the option of Show plots in tool window.
Try this
plt.imshow(im.reshape(im.shape[0], im.shape[1]), cmap=plt.cm.Greys)
It would help in some cases.
In my case image path was wrong! So firstly, you might want to check if image path is correct :)
Or maybe the image path contains Chinese characters, changing to English characters will solve this question.
For this kind of error try checking file path or name
As for cv2 is concerned.
You might not have provided the right file type while cv2.imread().
eg jpg instead of png.
Or you are providing image path instead
of image's array. eg plt.imshow(img_path),
try cv2.imread(img_path) first then plt.imshow(img) or cv2.imshow(img).
The problem was that my array was in type u3 i changed it to float and it worked for me .
I had a dataframe with Image column having the image/pic data.Reshaping part depends to person to person and image they deal with mine had 9126 size hence it was 96*96.
a = np.array(df_train.iloc[0].Image.split(),dtype='float')
a = a.reshape(96,96)
plt.imshow(a)
Input should be array
plt.imshow(plt.imread('image_path'))
For an image file in .mat format.
I have done the following to show the image using the imshow() function.
mat = scipy.io.loadmat('point05m_matrix.mat')
x = mat.get("matrix")
print(type(x))
print(len(x))
plt.imshow(x, extent=[0,60,0,55], aspect='auto')
plt.show()

opencv 2.4.0 laplacian different results depending on API used?

I'm using OpenCV 2.4.0 python bindings and I found that when calculating a laplacian of an image I get different results with the cv2 API from the cv2.cv API.
if I use cv2 API:
im_laplacian = cv2.Laplacian(im_gray, cv2.IPL_DEPTH_32F, ksize = 3)
im_laplacian is always uint8 (missing sign), and ddepth has to be IPL_DEPTH_32F or IPL_DEPTH_64F, if I try IPL_DEPTH_16S or IPL_DEPTH_32S I get an error:
"OverflowError: Python int too large to convert to C long"
if I use cv2.cv API:
cvgray = cv.fromarray(im_gray)
im_laplacian2 = cv.CreateImage(cv.GetSize(cvgray), cv.IPL_DEPTH_16S, 1)
cv.Laplace(cvgray, im_laplacian2, 3)
as expected I get a signed laplacian, this is the same result as in the C++ API.
If I do:
im_laplacian2_scaled = cv.CreateImage(cv.GetSize(cvgray), 8, 1)
cv.ConvertScaleAbs(dst, im_laplacian2_scaled, 1, 0)
im_laplacian2_scaled is still different from im_laplacian calculated with cv2 API
In my particular case I think I can get away with the cv2 output,
but I'm puzzeled, shouldn't all APIs produce the same output?
do they use different algorithms?
or maybe the cv2 python bindings don't correspond to individual C++ functions but some combination of them?
New cv2 API uses different depth constants:
cv2.CV_64F instead of cv2.IPL_DEPTH_64F
cv2.CV_32F instead of cv2.IPL_DEPTH_32F
cv2.CV_32S instead of cv2.IPL_DEPTH_32S
cv2.CV_16S instead of cv2.IPL_DEPTH_16S
cv2.CV_16U instead of cv2.IPL_DEPTH_16U
cv2.CV_8S instead of cv2.IPL_DEPTH_8S
cv2.CV_8U instead of cv2.IPL_DEPTH_8U

python opencv imwrite ... can't find params

I am using opencv with python. I wanted to do an cv2.imwrte:
cv2.imwrite('myimage.png', my_im)
The only problem is that opencv does not recognize the params constants:
cv2.imwrite('myimage.png', my_im, cv2.CV_IMWRITE_PNG_COMPRESSION, 0)
It cannot find CV_IMWRITE_PNG_COMPRESSION at all. Any ideas?
I can't find key CV_XXXXX in the cv2 module:
Try cv2.XXXXX
Failing that, use cv2.cv.CV_XXXXX
In your case, cv2.cv.CV_IMWRITE_PNG_COMPRESSION.
More info.
The docs for OpenCV (cv2 interface) are a bit confusing.
Usually parameters that look like CV_XXXX are actually cv2.XXXX.
I use the following to search for the relevant cv2 constant name. Say I was looking for CV_MORPH_DILATE. I'll search for any constant with MORPH in it:
import cv2
nms = dir(cv2) # list of everything in the cv2 module
[m for m in nms if 'MORPH' in m]
# ['MORPH_BLACKHAT', 'MORPH_CLOSE', 'MORPH_CROSS', 'MORPH_DILATE',
# 'MORPH_ELLIPSE', 'MORPH_ERODE', 'MORPH_GRADIENT', 'MORPH_OPEN',
# 'MORPH_RECT', 'MORPH_TOPHAT']
From this I see that MORPH_DILATE is what I'm looking for.
However, sometimes the constants have not been moved from the cv interface to the cv2 interface yet.
In that case, you can find them under cv2.cv.CV_XXXX.
So, I looked for IMWRITE_PNG_COMPRESSION for you and couldn't find it (under cv2....), and so I looked under cv2.cv.CV_IMWRITE_PNG_COMPRESSION, and hey presto! It's there:
>>> cv2.cv.CV_IMWRITE_PNG_COMPRESSION
16
Expanding on mathematical.coffee to ignore case and look in both namespaces:
import cv2
import cv2.cv as cv
nms = [(n.lower(), n) for n in dir(cv)] # list of everything in the cv module
nms2 = [(n.lower(), n) for n in dir(cv2)] # list of everything in the cv2 module
search = 'imwrite'
print "in cv2\n ",[m[1] for m in nms2 if m[0].find(search.lower())>-1]
print "in cv\n ",[m[1] for m in nms if m[0].find(search.lower())>-1]
>>>
in cv2
['imwrite']
in cv
['CV_IMWRITE_JPEG_QUALITY', 'CV_IMWRITE_PNG_COMPRESSION', 'CV_IMWRITE_PXM_BINARY']
>>>
Hopefully this problem will go away in some later release of cv2...
the compression style is automatically chosen from the file extension. see the cv2.imwrite help here.
however you might still be interested to know all the possible flags used by all the possible functions in cv2 and cv modules.
look for cv2.txt and cv.txt on your computer. they will be where the opencv modules are installed. at the bottom of those text files are a list of the flags used by the respective modules.
just in case you don't find them, you can download the ones i have from here, though they are from august 2011:
cv2.txt
cv.txt
in fact, with cv2 style API, this constant is replaced with cv2.IMWRITE_PNG_COMPRESSION.

Categories