Is it possible same code returns different result

2019-08-17 00:21发布

问题:

I'm running python program with openCV on Raspberry-Pi.

I got super wierd OpenCV Error. It looks it must not be happend, but is happened.

In short, masks croped by cv2.inRange() is different with same image.

Simple structure of my code:

  1. process images into mask and calculate kind of accuracy.
  2. If mask is not gather in (1.), which means mask of image is all black(empty) np.array, it skip an iteration by exception: Value Error command.(Because min(empty np.array) in code araise Value Error.)
  3. Iterate (1.~2.) with time interval.

Problem:

I've got same OpenCV Error more than two weeks. It is because some parameters in codes are assigned with wrong value. This paramters are mostly calculated from mask that is cropped from image. So I saved all images and masks in the iterating procedure. And found that there were tiny mask that must not be made at the 9th iteration

Problem is that when I tired to crop mask with same 9 th image manually on my labtop, it makes no mask which means just empty(black) mask is made. but there were very small mask(white region) is made during iterative running in Raspberry-Pi.

I've checked if there are difference between files in labtop and R-Pi, but both were same. Here is the images: First is input image to make Real instance. Second is manually created mask. Thired is Automatically created mask in R-Pi.

Only difference between pc and R-Pi is that: when I run code manually in pc, I have used saved image.jpg from SDcard of R-Pi. and when code is run in R-Pi iteratively, image is directly captured from R-pi camera module V2

I know it is verry wierd, and many people would think there are typo or something wrong in codes. But I have been suffered for this problem from last two weeks. I need any thread about this kind of problem.

Is there any possible cases that different result is gathered from same code in openCV or R-pi?

Here is some part of code. Real instance is created iteratively in loop. The line occuring OpenCV Error is marked with #. If someone request, I will upload more. It is bit long to upload full code.

class Real:  
    __ob_low=np.array([25,100,50]) #27,65,100]) /25,100,50
    __ob_high=np.array([33,255,255]) #[45,255,255]) /33,255,255

    def __opening(self, mask):
        kernel = np.ones((7, 1), np.uint8)
        op = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
        return op

    def __init__(self, img_got,imgname):

        img = img_got
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

        mask_temp = cv2.inRange(hsv, self.__ob_low, self.__ob_high)
        mask = self.__opening(mask_temp)

        maskdir = "/home/pi/capstone/mask/" #
        img_name = imgname #
        cv2.imwrite(os.path.join(maskdir,img_name),mask) #

        if not (np.any(mask)):
            print("mask is empty")

        array1 = np.transpose(np.nonzero(mask))
        array2 = np.nonzero(mask)

        ymin=min(array2[1])
        ymax=max(array2[1])
        xmin=min(array2[0])
        xmax=max(array2[0])

        self.x = xmax-xmin
        self.y = ymax-ymin
        self.ratio = self.x/self.y

        self.img = img
        self.mask = mask[(xmin):(xmax),(ymin):(ymax)]

回答1:

It was because the image directly captured from camera is not same with image.jpg that is saved in disk. First one is more sensitive with cv2.Inrange(). It looks parts of image is lost during the saving process. If I have saved file as other format such as .png or else, result would different.

Anyway, to synchronize R-Pi and labtop, I have changed code to use saved .jpg file in both R-Pi and labtop.



回答2:

I had an issue with my code on a diffrent system and it turns out version 3.4.1 and 3.3.1 yields different result from my cropping algorithm!