Cannot show the original picture use opencv-python 3.4 - python

I'am a newer for opencv.
I used the next command install the opencv 3.4
py -3 -m pip install open_python-3.4.4.19-cp36-cp36m-win_ame64.whl
and use the code named Image_Thresholding.ipynb process the picture
the file Image_Thresholding.ipynb like this:
import cv2
pic = cv2.imread('adult.jpg', 0)
threshold_value = 200
(T_value, binary_threshold) = cv2.threshold(pic, threshold_value, 255, cv2.THRESH_BINARY)
cv2.imshow('binary', binary_threshold)
cv2.waitKey(0)
cv2.destroyAllWindows()
I used pycharm2018.3.1 and set the configurations to use Jupter Notebook to run up code show the processed the picture, It's a white and black picture but I want use the next code
to show the original picture it also show the processed white and black picture
import cv2
img = cv2.imread('adult.jpg', 0)
cv2.imshow('adult', img)
cv2.waitKey(0)
cv2.destoryAllWindows()
How to show the original picture. Is my environment wrong or any else problem ?

You're experiencing such issue because of the 0 flag you use in the imread function call. That is used to read images in grayscale.
In order to load and display a color image just do:
import cv2
img = cv2.imread('adult.jpg')
cv2.imshow('adult', img)
cv2.waitKey(0)
cv2.destoryAllWindows()
Hope this helps

Related

opencv imshow shows an empty window when ran

when i try to use cv2.imshow() it gives me an empty window with the title i gave it. i tried another image with a link to the image, that did not work either. When i run print(img.shape) it gives me the size, so the image is loaded. here is my code:
import cv2
img = cv2.imread('image.jpg')
cv2.imshow('img', img)
cv2.waitKey(0)
here is what i get when i run this script:
image
if anyone knows something about this then please let me know!

OpenCV not showing image in python

This is the code that I am using for OpenCV to display image. It only shows me a blank screen instead of showing a picture.
import cv2
# location and name of file is completely correct
img = cv2.imread("./Resources/img-2.jpg")
# Doesn't give a null so its okay
print(img.shape)
# suspecting that problem is here
cv2.imshow("preview", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
The image is stored in the right location and when I'm using a similar approach for a video and a webcam, it works perfectly.
The following is what the out is -
Try using matplotlib instead :
import matplotlib.pyplot as plt
import cv2
img = cv2.imread("./Resources/img-2.jpg")
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # convert img pixels to RGB format, so that matplotlib displays the image properly
plt.imshow(img)
plt.show()
If it still gives you a blank image, then the problem might come from your file or filename.

Why does pytesseract fail to recognise digits from image with darker background?

I've this python code which I use to convert a text written in a picture to a string, it does work for certain images which have large characters, but not for the one I'm trying right now which contains only digits.
This is the picture:
This is my code:
import pytesseract
from PIL import Image
img = Image.open('img.png')
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
result = pytesseract.image_to_string(img)
print (result)
Why is it failing at recognising this specific image and how can I solve this problem?
I have two suggestions.
First, and this is by far the most important, in OCR preprocessing images is key to obtaining good results. In your case I suggest binarization. Your images look extremely good so you shouldn't have any problem but if you do, then maybe you should try to binarize your images:
import cv2
from PIL import Image
img = cv2.imread('gradient.png')
# If your image is not already grayscale :
# img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
threshold = 180 # to be determined
_, img_binarized = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY)
pil_img = Image.fromarray(img_binarized)
And then try the ocr again with the binarized image.
Check if your image is in grayscale and uncomment if needed.
This is simple thresholding. Adaptive thresholding also exists but it is noisy and does not bring anything in your case.
Binarized images will be much easier for Tesseract to handle. This is already done internally (https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality) but sometimes things can be messed up and very often it's useful to do your own preprocessing.
You can check if the threshold value is right by looking at the images :
import matplotlib.pyplot as plt
plt.imshow(img, cmap='gray')
plt.imshow(img_binarized, cmap='gray')
Second, if what I said above still doesn't work, I know this doesn't answer "why doesn't pytesseract work here" but I suggest you try out tesserocr. It is a maintained python wrapper for Tesseract.
You could try:
import tesserocr
text_from_ocr = tesserocr.image_to_text(pil_img)
Here is the doc for tesserocr from pypi : https://pypi.org/project/tesserocr/
And for opencv : https://pypi.org/project/opencv-python/
As a side-note, black and white is treated symetrically in Tesseract so having white digits on a black background is not a problem.

Read image grayscale opencv 3.0.0-dev

I am trying to read images directly as black and white.
I recently updated my OpenCv version to 3.0.0-dev, and the code that I used before does not work anymore.
img = cv2.imread(f, cv2.CV_LOAD_IMAGE_GRAYSCALE)
works fine for 2.4 but does not work for the new version, as there is no field CV_LOAD_IMAGE_GRAYSCALE.
Any suggestions?
Note: I know that cv2.imread(f,0) will work, but I do not like having unnamed constants in my code.
Thanks!
The flag has been renamed to cv2.IMREAD_GRAYSCALE. Generally speaking, flags now have names prefixed in a manner that relates to the function to which they refer. (e.g. imread flags start with IMREAD_, cvtColor flags start with COLOR_, etc.)
Try this it works for me
import cv2
im_gray = cv2.imread('gray_image.png', cv2.IMREAD_GRAYSCALE)
thresh = 127
im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
cv2.imwrite('blackwhite.png', im_bw)
Try this, it works for me everytime
import cv2
gray_img = cv2.imread('img.png', 0)
cv2.imshow(gray_img)

Python opencv drawContours does not show anything

I followed the tutorial at this page but nothing seems to happen when the line cv2.drawContours(im,contours,-1,(0,255,0),3) is executed. I was expecting to see star.jpg with a green outline, as shown in the tutorial. Here is my code:
import numpy as np
import cv2
im = cv2.imread('C:\Temp\ip\star.jpg')
print im.shape #check if the image is loaded correctly
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im,contours,-1,(0,255,0),3)
pass
There are no error messages. star.jpg is the star from the above mentioned webpage.
I am using opencv version 2.4.8 and Python 2.7.
Is drawContours supposed to show an image on my screen? If so, what did I do wrong? If not, how do I show the image?
Thanks
Edit:
Adding the following lines will show the image:
cv2.imshow("window title", im)
cv2.waitKey()
waitKey() is needed otherwise the window will just show a gray background. According to this post, that's because waitKey() tells it to start handling the WM_PAINT event.
I had the same issue. I believe the issue is that the underlying image is 1-channel rather than 3-channel. Therefore, you need to set the color so it's nonzero in the first element (e.g. (255,0,0)).
i too had the same problem. The thing is it shows, but too dark for our eyes to see.
Solution:
change the colour from (0,255,0) (for some weird reason, i too had give exactly the same color!) to (128,255,0) (or some better brighter colour)
You have to do something to the effect of:
cv2.drawContours(im,contours,-1,(255,255,0),3)
cv2.imshow("Keypoints", im)
cv2.waitKey(0)
I guess your original image is in gray bit plane. Since your bit plane is Gray instead of BGR and so the contour is not showing up. Because it's slightly black and grey which you cannot distinguish. Here's the simple solution [By converting the bit plane]:
im=cv2.cvtColor(im,cv2.COLOR_GRAY2BGR)
cv2.drawContours(im,contours,-1,(0,255,0),3)

Categories

Resources