Skip to content

Instantly share code, notes, and snippets.

@jaskiratsingh2000
Created December 15, 2022 18:08
Show Gist options
  • Save jaskiratsingh2000/3d5193a978d5ee29f93d5d2e42954317 to your computer and use it in GitHub Desktop.
Save jaskiratsingh2000/3d5193a978d5ee29f93d5d2e42954317 to your computer and use it in GitHub Desktop.
Fix the Code

WHAT I'M TRYING TO DO? I am trying to select the cordinates points on mouse click using cv2.setMouseCallback() function and printing the cordinates of every mouse click. I have put on the condition that the cordinates will be printed only until the counter and counterm is less than equal to 5 for two different functons, namely clickEventCTscanImage(event, x, y, flags, param) and clickEventMRIscanImage(event, x, y, flags, param)respectively.

WHAT I WANT TO ACHIEVE? I want to achieve that when I have selected the 5 points in totall the specific image should get closed and the second image should get automatically open ups and asks to perform the same function. that means the CT Scan image after achieving 5 points should get closed automatically and MRI image should open up for selecting the points and on selecting the points it should also get closed and then further code should executed aftewr printing in a following way:

Output to achieve:

enter image description here

CODE SNIPPET

import argparse
import numpy as np 
import matplotlib.pyplot as plt
import matplotlib.cm as cm 
import cv2
import imageio
import scipy.ndimage as ndi
import os
from PIL import Image


class GetCoordsMRICT:
    def __init__(self, CTscanImage, MRIscanImage):
        self.counter = 0
        self.counterm = 0
        self.CTSacanCoords= []
        self.MRIScanCoords = []
        # Running the Functions as follows,

        cv2.imshow('CT Scan Image', CTscanImage)
        cv2.setMouseCallback('CT Scan Image', self.clickEventCTscanImage)
        #cv2.waitKey()
        cv2.imshow('MRI Scan Image', MRIscanImage)
        cv2.setMouseCallback('MRI Scan Image', self.clickEventMRIscanImage)
        cv2.waitKey()

    def clickEventCTscanImage(self, event, x, y, flags, param):
        if event == cv2.EVENT_LBUTTONDOWN:
            if self.counter <= 5:
                print(x,y)
                self.CTScanCoords.append([x,y])
                self.counter = self.counter + 1
            
        if self.counter == 5:
            print('[+] CT Scan Points Completed.')
            #cv2.destroyAllWindows()
            return 0

        # Define Click Function for CT Scan
    def clickEventMRIscanImage(self, event, x, y, flags, param):
        if event == cv2.EVENT_LBUTTONDOWN:
            if self.counterm <= 5:
                print(x,y)
                self.MRIScanCoords.append([x,y])
                self.counterm = self.counterm + 1
        if self.counterm == 5:
            print('[+] MRI Scan Points Completed.')
            cv2.destroyAllWindows()
            return 0


if __name__ == "__main__":

    arg = argparse.ArgumentParser()
    arg.add_argument("--pathInCTImage", help="Path to CT Scan Image")
    arg.add_argument("--pathInMRIImage", help="Path to MRI Scan Image")
    args = arg.parse_args()


    CTscanImage = cv2.imread(args.pathInCTImage)
    MRIscanImage = cv2.imread(args.pathInMRIImage) #MRI Image will be registered

    handler = GetCoordsMRICT(CTscanImage, MRIscanImage)


    print(f'[+] CT Scan Codes: {handler.CTScanCoords}')
    print(f'[+] MRI Scan Codes: {handler.MRIScanCoords}')

So, I want to get the output like "CT Scan Codes: [],[]...." and "MRI Scan Codes: [],[]...." which I'm not getting. Hence, on running the above I'm getting this below output and even the program doesn't ends after that. I'm not sure why it is printing the "[+] CT Scan Points Completed." statement. I observed that even when I move my cursor without click this statement keeps on printing. Please help me do that.

213 226
249 258
243 272
243 272
291 224
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
[+] CT Scan Points Completed.
148 213
148 213
148 213
148 213
148 213
[+] MRI Scan Points Completed.
Traceback (most recent call last):
  File "/Users/jaskiratsingh/Desktop/registration_fusion/registration.py", line 49, in clickEventMRIscanImage
    return MRIScanCoords
NameError: name 'MRIScanCoords' is not defined

I have mentioned above whatever I tried.

@jaskiratsingh2000
Copy link
Author

@OttomanZ I just used your exact code which you have provided abovce and I'm getting the same issue. That is when I run the code firstly, it only shows the CT image not the MRI image. and when I selects the points in CT image then after 5 points the CT image gets close and in the terminal it still doesn't get completed rather stuck there. I have shown everything in this video. Please take a look at here - https://youtu.be/HwNqtSjt5kM

@jaskiratsingh2000
Copy link
Author

Please let me know based on the above video I shared

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment