def rotate_to_horizontal(self, image):
     image = self.correct_dimensions(image, 1600)
     if self.determine_tray_orientation(image) == -1:
         image = self.rotate_image(image, -45)
     elif self.determine_tray_orientation(image) == 1:
         image = self.rotate_image(image, 45)
     return ImageProcUtil.crop_out_blackv2(image)
 def find_dot_locations(self, imagearray):
     imagearraytransformed,cent=ImageProcUtil.threshold_dots_withcenter(imagearray)
     #print('hi')
     #plt.imshow(imagearraytransformed)
     #plt.show()
     imagearraytransformed=self.noise_remover.remove_noise(imagearraytransformed)
     self.cent=cent
     #print('hi2')
     #plt.imshow(imagearraytransformed)
     #plt.show()
     img_gray=cv2.cvtColor(imagearraytransformed, cv2.COLOR_BGR2GRAY)
     ret,thresholded=cv2.threshold(img_gray,1,255,cv2.THRESH_BINARY)
     #dev,thresholded=pcv.fill(thresholded, thresholded, 5, 0)
     #cv2.imshow("hi",thresholded)
     ###cv2.waitKey(0)
     #cv2.destroyAllWindows()
     w1, contours, w2 = cv2.findContours(thresholded, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
     print(len(contours))
     withcont=cv2.drawContours(imagearraytransformed, contours, -1, (0,255,0),1)
     #plt.imshow(withcont)
     #plt.show()
     centers=self.find_centers_of_dots(contours)
     centersfinal=list()
     for i in centers:
         centersfinal.append(tuple([i[0]+self.cent[1], i[1]+self.cent[0]]))
         withcont=cv2.circle(imagearray, tuple([i[0]+self.cent[1], i[1]+self.cent[0]]), 5,(255,0,0))
     withcont=cv2.circle(withcont, tuple([self.cent[0], self.cent[1]]), 5, (0,0,255))
     #plt.imshow(withcont)
     #plt.show()
     return centersfinal
 def determine_dot_code_order(self, image):
     trays = self.shelf_segmenter.split(image)
     tray = trays[0]
     tray, _center = ImageProcUtil.threshold_dots3_slack(tray)
     tray = self.noise_removal.remove_noise(tray)
     tray_grey = cv2.cvtColor(tray, cv2.COLOR_BGR2GRAY)
     thresholded_tray_grey = cv2.threshold(tray_grey, 1, 255,
                                           cv2.THRESH_BINARY)[1]
     w1, contours, w2 = cv2.findContours(thresholded_tray_grey,
                                         cv2.RETR_TREE,
                                         cv2.CHAIN_APPROX_NONE)
     big_dot = max(contours, key=cv2.contourArea)
     moments = cv2.moments(big_dot)
     ycoords = int(moments['m10'] / moments['m00'])
     xcoords = int(moments['m01'] / moments['m00'])
     if ycoords <= int(tray.shape[1] / 2):
         return True
     else:
         return False
    def process_tray(self, tray_img_path,section_types):
        """Return list of tray objects"""
        #cropped=ImageProcUtil.crop_out_black(tray_img_path)
        #plt.imshow(cropped)
        #plt.show()
        tray_counter=0
        tray_struct=list()
        all_errors = list()
        trays=self.shelf_segmenter.split(cv2.imread(tray_img_path))
        errors=0
        for i in trays:

            #plt.imshow(i)
            #plt.show()
            try:
                #plt.imshow(i)
                #splt.show()
                pass
            except:
                continue
            #i=self.color_enhancement(i,0.4)
            #plt.imshow(i)
            #plt.show()]
            try:
                #plt.imshow(i)
                #plt.show()
                cleaned,centers_adjustment=ImageProcUtil.threshold_dots3_slack(i)
                cleaned = self.noise_removal.remove_noise2(cleaned)
                # cv2.imshow("hi",cleaned)
                # cv2.waitKey(0)
                # cv2.destroyAllWindows()
            except Exception as e:
                # cv2.imshow('hi',i)
                # cv2.waitKey(0)
                # cv2.destroyAllWindows()
                tray_counter += 1
                #print("errors",e)
                print(e)
                continue
    def add_to_registry(self, tray_img_path, section_types):
        os.chdir('/Users/gghosal/Desktop/gaurav/photos')
        tray_counter = 0
        tray_struct = list()
        trays = self.shelf_segmenter.split(cv2.imread(tray_img_path))
        for i in trays:
            cleaned = ImageProcUtil.threshold_dots(i)
            cleaned = self.noise_removal.remove_noise(cleaned)
            pots = self.potsegmenter.split_half_trays(i)
            potsfinal = list()
            for q in pots:
                if bool((q.shape[0] >= 100) and (q.shape[1] >= 100)):
                    potsfinal.append(q)
            pots = potsfinal
            try:
                tray_id = self.dotcodereader.read_image2(cleaned)
                cv2.imshow("hi", i)
                k = cv2.waitKey()
                a = input("Correct Sequence?")
                if not a == "c":
                    dot_description = a.split(",")
                    description = str()
                    for dotqualifier in dot_description:
                        description += dotqualifier
                    updated_dot_code_id = self.dotcodereader.translator.get(
                        description, "Error!Try again")
                    print("Corrected: ", updated_dot_code_id)
                    tray_id = updated_dot_code_id
                tray_struct.append(
                    tuple(tray_id, section_types[tray_counter % 3]))
                tray_counter += 1
            except:
                pass

            self.registry[
                os.path.split(tray_img_path)[1].split("_")[1] + "_" +
                os.path.split(tray_img_path)[1].split("_")[2] +
                os.path.split(tray_img_path)[1].split("_")[3]] = tray_struct
            #cleaned_grey=cv2.cvtColor(cleaned, cv2.COLOR_RGB2HSV)
            #cleaned_grey=cleaned_grey
            #cleaned_thresh=cv2.inRange(cleaned_grey,np.array([0,0,150]),np.array([255,255,255]))
            #cleaned=cv2.bitwise_and(cleaned, cleaned, mask=cleaned_thresh)
            #kernel = np.ones((7,7),np.uint8)
            #cleaned = cv2.morphologyEx(cleaned, cv2.MORPH_CLOSE,(31,31))

            

            
            
            #kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
            #im = cv2.filter2D(cleaned, -1, kernel)

            #cleaned=cv2.bitwise_and(cleaned, cleaned, mask=th3)
            cleaned=ImageProcUtil.cvtcolor_bgr_rgb(cleaned)
            
            #grey=cv2.cvtColor(cleaned, cv2.COLOR_BGR2GRAY)
            #_,thresh=cv2.threshold(grey, 1,255, cv2.THRESH_BINARY)
            #_,binarymask=cv2.threshold(grey, 80,255,cv2.THRESH_BINARY)
            #cleaned=cv2.bitwise_and(cleaned, cleaned, mask=binarymask)
            #binarymask=grey>binarymask

            #print(binarymask)
            
##            for i in range(binarymask.shape[0]):
##                for p in range(binarymask.shape[1]):
##                    if binarymask[i][p]:
##                        binarymask[i][p]=255
##                    else:
##                        binarymask[i][p]=0
for i in list(
        listdir_nohidden("/Users/gghosal/Desktop/gaurav_new_photos/Shelf3_1")):
    os.chdir("/Users/gghosal/Desktop/gaurav_new_photos/Shelf3_1")

    for j in shelf_segmenter.split(
            apply_brightness_contrast(
                cv2.imread('20131104_Shelf3_1900_1_masked.tif'),
                brightness=0,
                contrast=0)):
        try:
            #cleaned,_=ImageProcUtil.threshold_dots3(j)
            #cleaned=noise_removal.remove_noise(cleaned)
            cv2.imshow("hi", j)
            cv2.waitKey(0)
            cv2.destroyAllWindows()
            cleaned2, _ = ImageProcUtil.threshold_dots3_slack(j)

            cleaned2 = noise_removal.remove_noise(cleaned2)
            ##            cleaned2grey=cv2.cvtColor(cleaned2, cv2.COLOR_BGR2GRAY)
            ##            _,cleaned2thresh=cv2.threshold(cleaned2grey, 10, 255, cv2.THRESH_BINARY)
            ##            #print(cleaned2thresh)
            ##            w1,cnt,w2=cv2.findContours(cleaned2thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
            ##            #cnt=cnt[1:]
            ##            #cnt=cnt[0]
            ##            realcnt=[]
            ##            for c in cnt:
            ##                #print(c)
            ##                perimeter = cv2.arcLength(c, True)
            ##                area = cv2.contourArea(c)
            ##                circularity = 4*math.pi*(area/(perimeter*perimeter))
            ##                print(circularity)
            for contourtocheck in clusters1:
                if not obj_hierarchy[contourtocheck][2] == -1:
                    if not cv2.contourArea(
                            contours[obj_hierarchy[contourtocheck][2]]) >= 20:
                        obj_hierarchy[contourtocheck][2] = -1
        counter = 0
        for foundcontour in clusters_i:
            hierarchycontours = [obj_hierarchy[j][2] for j in foundcontour]
            if not all([bool(j == -1) for j in hierarchycontours]):
                dotQ.append(True)
            else:
                dotQ.append(False)
        return dotQ


if __name__ == '__main__':
    a = DotCodeReader("/Users/gghosal/Desktop/dotcodestranslated.dat", {
        'red': 0,
        "lightblue": 98,
        "darkblue": 120,
        "pink": 173,
        "purple": 160
    })
    b = NoiseRemoval.NoiseRemoval()
    FILE = "/Users/gghosal/Documents/GitHub/PlantTrayMeasuringPipeline/Figure_1.jpg"

    print(
        a.read_image2(
            b.remove_noise(ImageProcUtil.threshold_dots(
                pcv.readimage(FILE)[0]))))