I would like to get input when I am showing an image using OpenCV's imshow() function. But If I do so, the image doesn't show correctly and a grey image appears instead.
It works if I use destroyAllWindows() command, but is there a proper solution for this?
def write_solution(crop):
cv2.imshow('Frage',crop)
ans = input("Antwort: ")
cv2.waitKey(0)
cv2.destroyAllWindows()
return ans
Related
import cv2
img=cv2.imread('test.jpg')
cv2.imshow("frame1",img)
waitKey(0)
input_image
output_image
Above is my code and it is not giving expected result(complete image). Roughly 10% of image is getting as output.
My input image is of size 1.24MB.is there any size limitations in opencv??
Thanks for help in advance
The problem is arising from cv2.imshow() function. As you are using cv2 from python, the command to use would be cv2.namedWindow('image',cv2.WINDOW_NORMAL) before cv2.imshow(). This should solve your problem. I tried your code the following way and it worked for me.
import cv2
img=cv2.imread('input1.jpg')
cv2.namedWindow('image',cv2.WINDOW_NORMAL)
#cv2.resizeWindow('image', 600,600)
cv2.imshow("image",img)
k = cv2.waitKey(0)
if k == 27:
cv2.imwrite('newImage2.png', img)
cv2.destroyAllWindows()
See if this can solve your problem.
You can use the namedwindow() method to adjust the screen size to fit the image size.
Here is the function;
cv.NamedWindow(name, flags=CV_WINDOW_AUTOSIZE)
I've got two images, the first one contain multiple items, which shows true colors. Then when I removed most of the item, then the webcam tried to auto-balance the image and yielded really false color.
Is there a way (in code) to apply the color profile of the first (true-color) image to the second image?
(or point me to some keywords, I'm new to the field, thanks)
Attached them here for easy comparison
True color
Falsely-adjusted color
I used Logitech webcam, which I can't figure out how to turn off auto-balance in code (in Linux).
I use this method and it works very well:
#pip install color_transfer
from color_transfer import color_transfer
# Load the two images
img1 = cv2.imread('image12.png')
img2 = cv2.imread('image1.png')
# Apply the color transfer
img2_transferred = color_transfer(img1, img2)
cv2.imshow("image", img2_transferred)
if cv2.waitKey(0) == chr("q"):
exit(0)
enter image description here
I am new to the this image processing stuff. Why I am asking this question is because I have a code which works for RGB mode but doesnt for P mode ?
So I came to conclusion that it is something related to modes. I did some basic research on modes.but did not find any simple explanation. Will be helpful if someone can help me understand this.
CODE:
image=Image.open('image.png')
image.load()
image_data = np.asarray(image)
image_data_bw = image_data.max(axis=2)
non_empty_columns = np.where(image_data_bw.max(axis=0)>0)[0]
non_empty_rows = np.where(image_data_bw.max(axis=1)>0)[0]
cropBox = (min(non_empty_rows), max(non_empty_rows), min(non_empty_columns), max(non_empty_columns))
image_data_new = image_data[cropBox[0]:cropBox[1]+1, cropBox[2]:cropBox[3]+1 , :]
new_image = Image.fromarray(image_data_new)
new_image.save('cropped_image.png')
Codesource
Input to the code following Image:
Output should be like the following image(It is cropped to the edges of the picture. Please click on the image for understanding):
This Image is in RGBA mode.so the code is working fine for such images. But not with the image in P mode.
ERROR:
Error I get with P mode:
axis 2 is out of bounds for array of dimension 2
The answer you found greatly overcomplicates the process, by using numpy. The PIL library supports this usecase natively, with the image.getbbox() and image.crop() methods:
cropbox = image.getbbox()
new_image = image.crop(cropbox)
This works for all the different modes, regardless. The cropbox produced by image.getbbox() is exactly the same size as the one produced by the numpy route.
from PIL import Image
img = Image.open('Image.png')
print(x,y)
img.show()
cropbox_1 = img.getbbox()
new_image_1 = img.crop(cropbox_1)
new_image_1.save('Cropped_image,png')
new_image_1.show()
This code completely crops the image to the edges. Only if the images are having alpha channel, you might have to remove that channel by converting it.
ex. If it is a RGBA mode make it RGB and then use getbbox().
img = image.convert('RGB')
cropbox = img.getbbox()
image_1 = img.crop(cropbox)
addition of this should do the task.
So working with windows, python 2.7 and simplecv I am making a live video with my webcam and want simplecv to give me a grayscale version of the video. Is there any simple way to achieve that?
I found the command
grayscale()
on the opencv page, which should do exactly that but when I run it I get the error:
NameError: name "grayscale" is not defined
I am currently using this prewritten code for object tracking but I don't know whether I should use the command I found, and where in the code I should put it, does anybody have an idea? :
print __doc__
import SimpleCV
display = SimpleCV.Display()
cam = SimpleCV.Camera()
normaldisplay = True
while display.isNotDone():
if display.mouseRight:
normaldisplay = not(normaldisplay)
print "Display Mode:", "Normal" if normaldisplay else "Segmented"
img = cam.getImage().flipHorizontal()
dist = img.colorDistance(SimpleCV.Color.BLACK).dilate(2)
segmented = dist.stretch(200,255)
blobs = segmented.findBlobs()
if blobs:
circles = blobs.filter([b.isCircle(0.2) for b in blobs])
if circles:
img.drawCircle((circles[-1].x, circles[-1].y), circles[-1].radius(),SimpleCV.Color.BLUE,3)
if normaldisplay:
img.show()
else:
segmented.show()
There are multiple ways to do this in SimpleCV.
One way has been already described, it's the toGray() method.
There's also a way you can do this with gaussian blur, which also helps to remove image noise:
from SimpleCV import *
img = Image("simplecv")
img.applyGaussianFilter(grayscale=True)
After the third line, img object contains the image with a lot less high-frequency noise, and converted to grayscale.
You may check out pyimagesearch.com who works with OpenCV, but he explains why applying Gaussian Blur is a good idea.
In simple cv theres a function called toGray() for example:
import SimpleCV as sv
img = img.jpg
sv.img.jpg.toGray()
return gimg.jpg
Recently I have started learning opencv and python for image processing .I am facing problems with writing a function .
I was given a task as follows:
Write a function in python to open a color image and convert the image into grayscale.
You are required to write a function color_grayscale(filename,g) which takes two arguments:
a. filename: a color image (Test color image is in folder “Task1_Practice/test_images”. Pick first image to perform the experiment.)
b. g: an integer
Output of program should be a grayscale image if g = 1 and a color image otherwise.
The code i wrote is as follows :
import cv2
def color_grayscale(filename,g):
filename = cv2.imread("a15.jpg")
" Enter Value of g:"
if g == 1:
gray = cv2.cvtColor(filename, cv2.COLOR_BGR2GRAY)
img = cv2.imshow("gray",gray)
else:
img = cv2.imshow("original",filename)
return(img)
color_grayscale("a15.jpg",1)
The code when run gives no output whatsoever.
cv2.imshow should be followed by waitKey function which displays the image for specified milliseconds. Otherwise, it won’t display the image. For example, waitKey(0) will display the window infinitely until any keypress (it is suitable for image display). waitKey(25) will display a frame for 25 ms, after which display will be automatically closed. (If you put it in a loop to read videos, it will display the video frame-by-frame)
Just add cv2.waitKey(0) before you return img and then it will display the grayscale image