CV_8UC1 (Error-215) in function adaptiveThreshold - python

In this code we are preprocessing an RGB image for Tesseract OCR using tools such as cv2, NumPy and PIL. When this code is executed in the Python 2.7.13 Shell, I recieve the following error message.
Traceback (most recent call last): File "C:\Automation\OCR\images\OCR_Preprocessing_ RGB.py", line 23, in <module> cv2.THRESH_BINARY,11,2) error: C:\build\master_winpack-bindings-win32-vc14-static\opencv\modules\imgproc\src\thresh.cpp:1446: error: (-215) src.type() == CV_8UC1 in function cv::adaptiveThreshold
Here is the code that error generated from. I have marked lines of code where I think the problem may be.
import cv2
import numpy as np
from matplotlib import pyplot as plt
from cycler import cycler
from PIL import Image, ImageEnhance
# Loads the image then enhances it
image = Image.open('teleCapture.png')
contrast = ImageEnhance.Contrast(image)
img = contrast.enhance(2)
img = np.asarray(img)
r,g,b,a = cv2.split(img) // I know the issue is here, I have too many channels for an RGB image or I am merginf them wrong.
contrast = cv2.merge([b,g,r]) //"Contrast" was used as a src during Thresholding, is this what it should be?
# Adaptive Gaussian Thresholding //The problem may be within the thresholding, does this thresholding function only work using grayscale images?
th1 = cv2.adaptiveThreshold(contrast,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY,11,2)
# Otsu's thresholding
ret2,th2 = cv2.threshold(contrast,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# Otsu's thresholding after Gaussian filtering
blur = cv2.GaussianBlur(contrast,(5,5),0)
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# writes enhanced and thresholded img
cv2.imwrite('preprocessedTeleCapture.png', th2)

Threshold method need 1 channel image as input, and you give 3 channel, that's the problem shown in error message.

Related

reproduce same output with scikit-image resize and OpenCV resize function

I'm trying to reproduce the same output with these snippets:
Scikit-Image + Keras
from keras.models import model_from_json
import numpy as np
from skimage.io import imread
from skimage.transform import resize
image = resize(imread(img_path, as_grey=False), (80, 80), preserve_range=True, mode='constant')
image /= 255.
img_array = np.array([image])
pred_IN = model.predict(img_array)
OpenCV
import cv2
model = cv2.dnn.readNet('mynet.prototxt', 'mynet.caffemodel')
image = cv2.imread(image_path)
img = cv2.dnn.blobFromImage(image, scalefactor=(1.0/255.0), size=(80, 80), swapRB=True, crop=False)
model.setInput(img)
pred = model.forward()
The problem is that I cannot get the same data to pass to the network (DNN module in case of OpenCV). Network is the same, input data is the same, but the results is slightly different and the reason is that resize function behaves differently between scikit-learn and OpenCV (used internally by blobFromImage) and don't know how to adapt the OpenCV code to match scikit-learn.
My final application will use OpenCV in C++, so I need to match this snippets, as my network has been trained with data generated by scikit-learn.
I think the reason is skimage use antialiasing (gaussian blur from scipy.ndimage before rescale) by default. You can achieve similar result wit resize in OpenCV by blurring your image (e.g. using cv2.GaussianBlur) before cv2.resize. Result from resize is not the same but with proper blur kernel size is very very similar (almost identical). Hope it'll help :)

opencv read image assertion failed

I am a newbie to python and opencv.
trying to read image.
here is my code :-
import cv2
import numpy
img = cv2.imread('Test1.jpg',0)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
here is the error generated :-
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /opt/concourse/worker/volumes/live/68762549-a7cd-401a-4fc4-6547354af396/volume/opencv_1512680491081/work/modules/highgui/src/window.cpp,
line 331 Traceback (most recent call last): File
"/Users/vinayak/PycharmProjects/Python_test1/test_img.py", line 4, in
cv2.imshow('image',img) cv2.error: /opt/concourse/worker/volumes/live/68762549-a7cd-401a-4fc4-6547354af396/volume/opencv_1512680491081/work/modules/highgui/src/window.cpp:331:
error: (-215) size.width>0 && size.height>0 in function imshow
please help me identify the fault. thanks in advance !
This error means you didnĀ“t loaded the image and img is empty.
There is a known error in imread for cv2. You can try replacing the imread call with this portion :
import matplotlib.pyplot as plt
img = plt.imread('Test1.jpg')
matplotlib does not have the same error on cv2.
The code is correct, this is the right way to load and display an image unsing OpenCV in Python, the additional argument you passed (0) means you're loading the image as grayscale and is the numeric value for the enum cv.IMREAD_GRAYSCALE
opencv load image tutorial
link to enums
Just for the sake of completeness, here the basic code I tested
import cv2 as cv
img = cv.imread('C:\\path\\to\\my\\Image\\image.bmp',0)
cv.imshow('image',img)
cv.waitKey(0)
cv.destroyAllWindows()
Due to this, the issue is most probably related to the path your're passing to cv.imread function
Are you sure your image is in the same working directoy you're
using?(and I bet is not...)
Did you try to change the relative path you gave with a complete absolute
path?

Run into this error while using Pillow

I am using Pillow (4.3.0) to perform image quantization. When I run the image quantization function in PIL, I get this error saying ValueError: dependency required by this method was not enabled at compile time
Here is a MWE of my code
from PIL import Image
import numpy as np
I = Image.open('test.jpeg')
Q = Image.Image.quantize(I, colors = 64,method = 3, kmeans = 0, palette = None)
I is an RGB image (32x32x3). I am using Elementary OS. How do I sort this error out?

Why can't I create an image and set it as the background in OpenCV?

So, this is what I am trying:
import cv2
import cv2.cv as cv
cv2.namedWindow(threeDWinName, cv2.CV_WINDOW_AUTOSIZE)
img2 = cv.CreateImage((320, 240), 32, 1)
cv2.imshow(threeDWinName,img2)
Does anybody know what is going wrong with this? I get TypeError: <unknown> is not a numpy array
Thanks
The more recent version of OpenCV, cv2 uses numpy arrays for images, the preceding version cv used opencv's special Mat's. In your code you've created an image as a Mat using the old cv function CreateImage, and then tried to view it using the newer cv2.imshow function, but cv2.imshow expects a numpy array...
...so all you need to do is import numpy, and then change you CreateImage line to:
img2 = np.zeros((320,240),np.float32)
And then it should be fine :)

Why doesn't cv2 dilate actually affect my image?

So, I'm generating a binary (well, really gray scale, 8bit, used as binary) image with python and opencv2, writing a small number of polygons to the image, and then dilating the image using a kernel. However, my source and destination image always end up the same, no matter what kernel I use. Any thoughts?
from matplotlib import pyplot
import numpy as np
import cv2
binary_image = np.zeros(image.shape,dtype='int8')
for rect in list_of_rectangles:
cv2.fillConvexPoly(binary_image, np.array(rect), 255)
kernel = np.ones((11,11),'int')
dilated = cv2.dilate(binary_image,kernel)
if np.array_equal(dilated, binary_image):
print("EPIC FAIL!!")
else:
print("eureka!!")
All I get is EPIC FAIL!
Thanks!
So, it turns out the problem was in the creation of both the kernel and the image. I believe that openCV expects 'uint8' as a data type for both the kernel and the image. In this particular case, I created the kernel with dtype='int', which defaults to 'int64'. Additionally, I created the image as 'int8', not 'uint8'. Somehow this did not trigger an exception, but caused the dilation to fail in a surprising fashion.
Changing the above two lines to
binary_image = np.zeros(image.shape,dtype='uint8')
kernel = np.ones((11,11),'uint8')
Fixed the problem, and now I get EUREKA! Hooray!

Categories

Resources