Run into this error while using Pillow - python

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?

Related

Error using OpenCV for Connected Components on numpy arrays

I'm trying to do a connected component analysis on an image using OpenCV in python. The images are in a '.czi' format so I am using aicsimageio to import the file into numpy array. However, I keep receiving the error:
OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\connectedcomponents.cpp:5632: error: (-215:Assertion failed) iDepth == CV_8U || iDepth == CV_8S in function 'cv::connectedComponents_sub1'
Below is some code similar to what I am trying to do. I will just create an numpy array for simplicity (instead of importing the image).
import numpy as np
import cv2
test = np.random.rand(512,512)
(T, thresh) = cv2.threshold(test, 0.5, 255, cv2.THRESH_BINARY)
num_labels, labels = cv2.connectedComponents(thresh)
This returns the error described above on the final line of code. The error seems to be something to do with the fact that the image wasn't imported using OpenCV, as I have got the same code to work when importing a random '.jpg' using OpenCV. I'm not sure why, since importing the '.jpg' using OpenCV returns a numpy array, i.e.:
image = cv2.imread(file)
type(thresh) == type(image)
returns True
Any help on how to fix this would be much appreciated!

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 :)

CV_8UC1 (Error-215) in function adaptiveThreshold

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.

compare histogram comparison method isn't working in opencv 3.1.0

I have to compare two images using cv2.compareHist() function. But I got error on the comparison method i.e. CV_COMP_CORREL. I use OpenCV 3.1.0.
Error is NameError: name 'CV_COMP_CORREL' is not defined.
I tried with cv.CV_COMP_CORREL and cv2.cv.CV_COMP_CORREL, but I got the same type error.
Here is my code:
import cv2
import numpy as np
from matplotlib import pyplot as plt
image = cv2.imread("29.jpg",0)
image1 = cv2.imread("29.jpg",0)
hist1 = cv2.calcHist([image],[0],None,[256],[0,256])
hist2 = cv2.calcHist([image1],[0],None,[256],[0,256])
compare = cv2.compareHist(hist1,hist2,CV_COMP_CORREL)
If you're still struggling with this; I found the answer, searching through the cv2.__dict__ dictionary:
for option in cv2.__dict__:
if 'CORREL' in option:
print option
I find cv2.HISTCMP_CORREL.

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 :)

Categories

Resources