Ask Your Question
0

Mouse click x and y coordinates are consistently incorrect on Linux[SOLVED]

asked 2020-02-20 00:02:13 -0600

updated 2020-02-22 11:03:41 -0600

supra56 gravatar image
System information (version)
  • OpenCV => :3.4.3.18
  • Operating System / Platform => Linux (Raspberry Pi 3 Model B)
  • Compiler => Thonny Python IDE

Hello,

I wrote a simple image display script in Python that cycles through 3 jpg images stored in the same folder. When I click on the right side of the picture, the next picture in the cycle should be displayed. When I click on the left side of the picture the previous image in the cycle should be displayed. The x coordinates on my screen range from 0 to ~1300 so I set a left-side click to be x<600 and a right-side click to be x>600.

My script works perfectly on a Windows 64 bit computer, however it doesn't work when I run it on my Raspberry Pi Model 3. I'm not sure if it's a Linux problem or if there's some sort of incompatibility with the Pi. When I click the right side of the image once, x correctly displayed as a value greater than 600. However, when I click the exact same spot again a second time, a value less than 600 is display. As I continue clicking the exact same spot, the x coordinate alternates between correct/incorrect x values .

In the screenshot here, I clicked the exact same spot on the right side of my image each time and printed out the associated x coordinate. As you can see, the x coordinate alternates between x>600 and x<600.

I have a similar problem with the y coordinates. Am I missing something? Any help is greatly appreciated, this really has me stumped. Thank you!

import cv2 #image processing
import glob #finds files in pathnames with certain parameters (ie jpg pictures)

#returns a list of all jpg images in filepath
images = [cv2.imread(file) for file in glob.glob("/home/pi/Pictures/*.jpg")]
i=0 #list index

def cycleForward():#cycle forward in the list
    global i, images
    if i==len(images)-1:
        i=0
    else:
        i+=1

def cycleBackward():#cycle backwards in the list
    global i, images
    if i==0:
        i=len(images)-1
    else:
        i-=1

def cycleImage(event,x,y,flag,param):#left mouse click to cycle through images
    global i, images 
    if event == cv2.EVENT_LBUTTONDOWN:
        if (x>600):#click the right side of the image to go to the next picture
            cycleForward() 
            print ("Forward "+str(x))  
        if (x<600):#click the left side of the image to go to the previous picture
            cycleBackward()
            print ("Backwards "+str(x))

#create fullscreen image
cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)

cv2.setMouseCallback("window", cycleImage)#monitor for left mouse click

while True:#continuously run
    img = images[i]
    cv2.imshow("window", img)#display one image
    key = cv2.waitKey(1) & 0xFF
    if key == 27:#press escape to stop image display
        cv2.destroyAllWindows() #destroys all created windows
        break
edit retag flag offensive close merge delete

Comments

1

Please remove screenshot and replace with a text version. "global i" you are a gamer!

LBerger gravatar imageLBerger ( 2020-02-20 00:30:55 -0600 )edit
1

By text version, he meant write out the code as part of the question. Not save it as a txt file and upload it ;)

eshirima gravatar imageeshirima ( 2020-02-20 08:09:22 -0600 )edit

WND_PROP_FULLSCREEN will resize the image to desktop size internally.

your windows and linux computers have a different screen resolution ? please check

berak gravatar imageberak ( 2020-02-20 09:04:46 -0600 )edit

Please mark your answer as solved rather than putting [SOLVED] in the title. Otherwise, this will stick around, cluttering up the list of unsolved questions.

ssokolow gravatar imagessokolow ( 2020-03-23 20:56:25 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-02-22 10:11:39 -0600

supra56 gravatar image

updated 2020-02-22 10:23:06 -0600

The problem has been solved. Don't set values too highly x>600 and x<600, Set minimum to x>100 and x>100 Do net set below values, otherwise freezing. As @berak said, you can set resize by using cv2.WND_PROP_FULLSCREEN // 2.

#left mouse click to cycle through images

    def cycleImage(event, x, y, flag, param): 
        global i, images
        #click the right side of the image to go to the next picture
        if event == cv2.EVENT_LBUTTONDOWN:
            if (x > 100): 
                cycleForward() 
                print ("Forward "+ str(x))
            #click the left side of the image to go to the previous picture    
            if (x < 100): 
                cycleBackward()
                print ("Backwards "+ str(x))

#create fullscreen image
cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN//2,cv2.WINDOW_FULLSCREEN//2)

I tested it perfectly.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-02-19 23:54:30 -0600

Seen: 1,144 times

Last updated: Feb 22 '20