python类THRESH_BINARY_INV的实例源码

cloud_detection.py 文件源码 项目:pynephoscope 作者: neXyon 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def detect(self, image, mask = None):
        floatimage = np.float32(image)

        fb,fg,fr = cv2.split(floatimage)

        nonzero = fr != 0
        difference = np.zeros(fr.shape, np.float32)
        difference[nonzero] = fb[nonzero] / fr[nonzero]
        _, result = cv2.threshold(difference, Configuration.br_ratio_threshold, 1, cv2.THRESH_BINARY_INV)
        return np.uint8(result)
cloud_detection.py 文件源码 项目:pynephoscope 作者: neXyon 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def detect(self, image, mask = None):
        floatimage = np.float32(image)

        fb,fg,fr = cv2.split(floatimage)

        nonzero = (fr + fb) != 0
        difference = np.zeros(fr.shape, np.float32)
        difference[nonzero] = (fb[nonzero] - fr[nonzero]) / (fb[nonzero] + fr[nonzero])
        _, result = cv2.threshold(difference, Configuration.nbr_threshold, 1, cv2.THRESH_BINARY_INV)
        return np.uint8(result)
squeeze.py 文件源码 项目:EvadeML-Zoo 作者: mzweilin 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def adaptive_binarize_py(x, block_size=5, C=33.8):
    "Works like an edge detector."
    # ADAPTIVE_THRESH_GAUSSIAN_C, ADAPTIVE_THRESH_MEAN_C
    # THRESH_BINARY, THRESH_BINARY_INV
    import cv2
    ret_imgs = opencv_wrapper(x, cv2.adaptiveThreshold, [255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, block_size, C])
    return ret_imgs
cv_functions.py 文件源码 项目:R-CNN_LIGHT 作者: YeongHyeon 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def adaptiveThresholding(gray=None, neighbor=5, blur=False, k_size=3):

    if(blur):
        gray = cv2.GaussianBlur(gray, (k_size, k_size), 0)
    return cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, neighbor, 1)
Imgdb.py 文件源码 项目:osrmacro 作者: jjvilm 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def turnBinary(self, img, *args):
        """Pass image as graysacle, else will be converted, other args include 'a' - add to DB, 'inv'-inverting, 's'-show"""
        #makes sure img is grayscale
        if len(img.shape) == 3:
            img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        _,img = cv2.threshold(img, 254,255,cv2.THRESH_BINARY)
        print(img.shape)
        print("Turned Binary")
        try:
            for arg in args:
                # adds passed image to image db
                if arg == 'a':
                    img_name = raw_input("Name for image\n")
                    self.pickled_dict[img_name] = img
                    self.savePickledDict()
                # inverts binary img
                if arg == 'inv':
                    img_name = raw_input("Name for image\n")
                    _, img = cv2.threshold(img,0,255,cv2.THRESH_BINARY_INV)
                # shows img
                if arg == "s":
                    cv2.imshow('img',img)
                    cv2.waitKey(0)
        except:
            pass
        return img
ImageProcessor.py 文件源码 项目:CVMazeRunner 作者: M-Niedoba 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def getThresholdedImage(self,isInverted):
        threshType = cv2.THRESH_BINARY_INV if isInverted else cv2.THRESH_BINARY
        retval, threshold = cv2.threshold(self.imageToProcess,127,255,threshType)
        return threshold
ImageProcessor.py 文件源码 项目:CVMazeRunner 作者: M-Niedoba 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def getOtsuBinarizedImage(self,isInverted):
        image = self.blurImage()
        threshType = cv2.THRESH_BINARY_INV if isInverted else cv2.THRESH_BINARY
        retval, threshed = cv2.threshold(image,0,255,threshType + cv2.THRESH_OTSU)
        return threshed
generater.py 文件源码 项目:Machine-Learning 作者: grasses 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def cutting(self, writer, fname):
        fpath = '{:s}/{:s}/{:s}'.format(self.conf.source_path, writer, fname)
        img = cv2.imread(fpath)
        if img is None: return
        img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        img_gray = cv2.GaussianBlur(img_gray, (5, 5), 0)
        ret, im_th = cv2.threshold(img_gray, 90, 255, cv2.THRESH_BINARY_INV)
        ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        rects = [cv2.boundingRect(ctr) for ctr in ctrs]

        if self.debug:
            print('=> file path = {:s}'.format(fpath))

        for i, rect in enumerate(rects):
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.rectangle(img, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3)
            cv2.putText(img, '({:d},{:d})'.format(rect[0], rect[1]), (rect[0], rect[1]), font, 0.8, (0, 255, 0), 2)
            cv2.putText(img, '({:d},{:d})'.format(rect[0] + rect[2], rect[1] + rect[3]), (rect[0] + rect[2], rect[1] + rect[3]), font, 0.8, (0, 255, 0), 2)

            leng = int(rect[3] * 1.6)
            pt1 = int(rect[1] + rect[3] // 2 - leng // 2)
            pt2 = int(rect[0] + rect[2] // 2 - leng // 2)

            if pt1 < 0 or pt2 < 0: continue

            roi = im_th[pt1:pt1 + leng, pt2:pt2 + leng]

            print("i = {:d} leng = {:.0f} pt1 = {:d} pt2 = {:d} rect[0] = {:d} rect[1] = {:d} rect[2] = {:d} rect[3] = {:d}".format(i, leng, pt1, pt2, rect[0], rect[1], rect[2], rect[3]))

            from matplotlib import pyplot
            import matplotlib as mpl
            fig = pyplot.figure()
            ax = fig.add_subplot(1, 1, 1)
            imgplot = ax.imshow(roi, cmap=mpl.cm.Greys)
            imgplot.set_interpolation('nearest')
            ax.xaxis.set_ticks_position('top')
            ax.yaxis.set_ticks_position('left')
            #pyplot.show()

            roi = cv2.resize(roi, (28, 28), interpolation = cv2.INTER_AREA)
            roi = cv2.dilate(roi, (3, 3))
            #roi_hog_fd = hog(roi, orientations = 9, pixels_per_cell = (14, 14), cells_per_block = (1, 1), visualise = False)
            cv2.imwrite('{:s}/{:s}/img.{:d}.{:.2f}.jpg'.format(self.conf.train_path, writer, i, time.time()), roi)
        cv2.imwrite('{:s}/img.{:d}.jpg'.format(self.conf.tmp_path, i), img)
scanchars.py 文件源码 项目:handfontgen 作者: nixeneko 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def splitimage(image):
    dpmm = min(image.shape[0:2]) / DOCSIZE[0]
    sizethresh = SIZE_THRESH_MM * dpmm

    uprightimg = makeupright(image)
    grayimg = getgrayimage(uprightimg)

    # top line
    top = grayimg[0,:]
    sepx = [0,]
    ret, binimg = cv2.threshold(top,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(binimg)
    for i in range(1,nlabels):
        if stats[i,cv2.CC_STAT_AREA] >= sizethresh:
            sepx.append(centroids[i][1])

    # left line 
    left = grayimg[:,0]
    sepy = [0,]
    ret, binimg = cv2.threshold(left,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(binimg)
    for i in range(1,nlabels):
        if stats[i,cv2.CC_STAT_AREA] >= sizethresh:
            sepy.append(centroids[i][1])

    # divide into images
    imgs = []
    for iy in range(len(sepy)):
        for ix in range(len(sepx)):
            if iy == len(sepy) - 1:
                if ix == len(sepx) - 1:
                    #right-bottom corner
                    imgs.append(uprightimg[int(sepy[iy]):,int(sepx[ix]):])
                else:
                    #bottom end
                    imgs.append(uprightimg[int(sepy[iy]):,int(sepx[ix]):int(sepx[ix+1])])
            else:
                if ix == len(sepx) - 1:
                    #right end
                    imgs.append(uprightimg[int(sepy[iy]):int(sepy[iy+1]),int(sepx[ix]):])
                else:
                    #others
                    imgs.append(uprightimg[int(sepy[iy]):int(sepy[iy+1]),int(sepx[ix]):int(sepx[ix+1])])

    return imgs
main.py 文件源码 项目:Automatic-Plate-Number-Recognition-APNR 作者: kagan94 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def find_contours(img):
    '''
    :param img: (numpy array)
    :return: all possible rectangles (contours)
    '''
    img_blurred = cv2.GaussianBlur(img, (5, 5), 1)  # remove noise
    img_gray = cv2.cvtColor(img_blurred, cv2.COLOR_BGR2GRAY)  # greyscale image
    # cv2.imshow('', img_gray)
    # cv2.waitKey(0)

    # Apply Sobel filter to find the vertical edges
    # Find vertical lines. Car plates have high density of vertical lines
    img_sobel_x = cv2.Sobel(img_gray, cv2.CV_8UC1, dx=1, dy=0, ksize=3, scale=1, delta=0, borderType=cv2.BORDER_DEFAULT)
    # cv2.imshow('img_sobel', img_sobel_x)

    # Apply optimal threshold by using Oslu algorithm
    retval, img_threshold = cv2.threshold(img_sobel_x, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)
    # cv2.imshow('s', img_threshold)
    # cv2.waitKey(0)

    # TODO: Try to apply AdaptiveThresh
    # Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.
    # gaus_threshold = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 115, 1)
    # cv2.imshow('or', img)
    # cv2.imshow('gaus', gaus_threshold)
    # cv2.waitKey(0)

    # Define a stuctural element as rectangular of size 17x3 (we'll use it during the morphological cleaning)
    element = cv2.getStructuringElement(shape=cv2.MORPH_RECT, ksize=(17, 3))

    # And use this structural element in a close morphological operation
    morph_img_threshold = deepcopy(img_threshold)
    cv2.morphologyEx(src=img_threshold, op=cv2.MORPH_CLOSE, kernel=element, dst=morph_img_threshold)
    # cv2.dilate(img_threshold, kernel=np.ones((1,1), np.uint8), dst=img_threshold, iterations=1)
    # cv2.imshow('Normal Threshold', img_threshold)
    # cv2.imshow('Morphological Threshold based on rect. mask', morph_img_threshold)
    # cv2.waitKey(0)

    # Find contours that contain possible plates (in hierarchical relationship)
    contours, hierarchy = cv2.findContours(morph_img_threshold,
                                           mode=cv2.RETR_EXTERNAL,  # retrieve the external contours
                                           method=cv2.CHAIN_APPROX_NONE)  # all pixels of each contour

    plot_intermediate_steps = False
    if plot_intermediate_steps:
        plot(plt, 321, img, "Original image")
        plot(plt, 322, img_blurred, "Blurred image")
        plot(plt, 323, img_gray, "Grayscale image", cmap='gray')
        plot(plt, 324, img_sobel_x, "Sobel")
        plot(plt, 325, img_threshold, "Threshold image")
        # plot(plt, 326, morph_img_threshold, "After Morphological filter")
        plt.tight_layout()
        plt.show()

    return contours
wordart.py 文件源码 项目:csss-minion 作者: henrymzhao 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def avatart(self, ctx, *args):
        """Make a wordcloud in the shape of your avatar.
        usage: !avatart <invert> <bgcolor>

        """
        fmt = "Making artwork {}, hold your horses!"
        msg = await self.bot.say(fmt.format(ctx.message.author.mention))
        fin_img = path.join(self.d,self.e,"fin.png")

        # this whole block is lol
        if len(args) >= 2:
            if args[0] == "yes" or args[0] == "true" or args[0] == "invert":
                thresh = cv2.THRESH_BINARY_INV
            else:
                thresh = cv2.THRESH_BINARY
            try:
                if Color(args[1]):
                    bg_colour = args[1]
            except:
                bg_colour = "white"
        elif len(args) == 1:
            try:
                if Color(args[0]):
                    bg_colour = args[0]
            except:
                bg_colour = "white"
            if args[0] == "yes" or args[0] == "true" or args[0] == "invert":
                thresh = cv2.THRESH_BINARY_INV
            else:
                thresh = cv2.THRESH_BINARY
        else:
            thresh = cv2.THRESH_BINARY
            bg_colour = "white"     
        ava = ctx.message.author.avatar_url # grab avatar URL
        try:
            if ava == "":
                print("there's no avatar for this user: "+str(ctx.message.author))
                await self.bot.say("```I can't make avatar art without an avatar you silly goose. But it's ok, I have something special for you.```")
                img = cv2.imread(path.join(self.d,self.e,"default_avatar.jpg"),1)
            else:
                img_data = requests.get(ava, stream=True).content #dl from dat url
                img = cv2.imdecode(np.frombuffer(img_data, np.uint8),1) # convert from string butter to uint8    

            img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # grayscale that motha
            ret,img_bw = cv2.threshold(img_gray,127,255, thresh) #threshold values
            scaled = cv2.resize(img_bw, (1024,1024), interpolation = cv2.INTER_LINEAR)

            word = self.wordsFromDB(ctx.message.author) # retrieve words from DB
            text = " ".join(word)
            avatar_mask = np.array(scaled) # create mask
            wc = WordCloud(background_color=bg_colour, max_words=20000,stopwords=self.STOPWORDS, mask=avatar_mask)
            wc.generate(text)
            wc.to_file(fin_img) # save masked wordart to file
            await self.bot.send_file(ctx.message.channel, fin_img, content=ctx.message.author.mention)
            await self.bot.delete_message(msg)
        except:
            await self.bot.say("```Something has gone horribly wrong.```")



    # only refresh cache if an authorized ID
camshift.py 文件源码 项目:OpenCV2 作者: SarathM1 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def count_fingers(img):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Otsu's thresholding after Gaussian filtering
    img = cv2.GaussianBlur(img, (5, 5), 0)
    ret, mask = cv2.threshold(img, 0, 255,
                              cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    cv2.imshow("Threshold", mask)

    (_, cnts, _) = cv2.findContours(mask,
                                    cv2.RETR_EXTERNAL,
                                    cv2.CHAIN_APPROX_SIMPLE)
    list_far = []
    list_end = []
    if cnts:
        areas = [cv2.contourArea(c) for c in cnts]
        max_index = np.argmax(areas)
        cnt = cnts[max_index]

        M = cv2.moments(cnt)
        cx = int(M['m10']/M['m00'])
        cy = int(M['m01']/M['m00'])

        hull1 = cv2.convexHull(cnt)

        hull2 = cv2.convexHull(cnt, returnPoints=False)

        try:
            defects = cv2.convexityDefects(cnt, hull2)
        except Exception,  e:
            defects = None
            print e

        counter = 0
        if defects is not None:
            for i in range(defects.shape[0]):
                s, e, f, d = defects[i, 0]
                # start = tuple(cnt[s][0])
                end = tuple(cnt[e][0])
                far = tuple(cnt[f][0])
                if d < 20000:
                    continue
                if far[1] >= (cy+40):
                    continue

                diff1 = abs(end[0]-far[0])
                if diff1 > 100:
                    continue

                cv2.line(img, end, far, (0, 0, 0), 2, 8)
                cv2.imshow("hand", img)
                cv2.waitKey(1)
                list_far.append(far)
                list_end.append(end)
                counter += 1

    return mask, counter, hull1, (cx, cy), list_far, list_end
dip.py 文件源码 项目:OpenCV2 作者: SarathM1 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def checkButton(self, img, x1, y1, x2, y2):
        btn1 = img[y1:y2, x1:x2]
        btn1 = cv2.cvtColor(btn1, cv2.COLOR_BGR2GRAY)

        if self.thresh_change_trigger:
            ret, mask = cv2.threshold(btn1, 0, 255, cv2.THRESH_BINARY_INV +
                                      cv2.THRESH_OTSU)
            self.thresh_val.setText(str(ret))
            self.THRESH = ret
        else:
            ret, mask = cv2.threshold(btn1, self.THRESH, 255,
                                      cv2.THRESH_BINARY_INV)
        try:
            (_, cnts, _) = cv2.findContours(mask, cv2.RETR_EXTERNAL,
                                            cv2.CHAIN_APPROX_SIMPLE)
        except Exception, e:
            (cnts, _) = cv2.findContours(mask, cv2.RETR_EXTERNAL,
                                            cv2.CHAIN_APPROX_SIMPLE)

        ci = 0
        max_area = 0

        if cnts:
            for i in range(len(cnts)):
                cnt = cnts[i]
                area = cv2.contourArea(cnt)
                if(area > max_area):
                    max_area = area
                    ci = i
            cnt = cnts[ci]

        else:
            cnt = None

        self.flags.isSet_prev = self.flags.isSet_cur
        if cnt is not None:
            cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 0), 1)
            hull = cv2.convexHull(cnt)
            cv2.drawContours(btn1, [hull], 0, (0, 0, 255), 1)
            self.flags.isSet_cur = True
        else:
            cv2.rectangle(img, (x1, y1), (x2, y2), (188, 188, 137), 1)
            self.flags.isSet_cur = False
        return img
openCVgui.py 文件源码 项目:OpenCV2 作者: SarathM1 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def count_fingers(hand_frame):
    hand_frame = cv2.cvtColor(hand_frame,cv2.COLOR_BGR2GRAY)

    # Otsu's thresholding after Gaussian filtering
    hand_frame = cv2.GaussianBlur(hand_frame,(5,5),0)
    ret,mask = cv2.threshold(hand_frame,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

    (cnts,_)=cv2.findContours(mask.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

    list_far=[]
    list_end=[]
    if cnts:
        areas = [cv2.contourArea(c) for c in cnts]
        max_index = np.argmax(areas)
        cnt=cnts[max_index]

        M = cv2.moments(cnt)
        cx = int(M['m10']/M['m00'])
        cy = int(M['m01']/M['m00'])

        hull1 = cv2.convexHull(cnt)

        hull2 = cv2.convexHull(cnt,returnPoints = False)

        try:
            defects = cv2.convexityDefects(cnt,hull2)
        except Exception, e:
            defects = None
            print e

        counter = 0
        if defects is not None:
            for i in range(defects.shape[0]):
                s,e,f,d = defects[i,0]
                start = tuple(cnt[s][0])
                end = tuple(cnt[e][0])
                far = tuple(cnt[f][0])
                if d<20000:
                    continue

                if far[1] >= (cy+40):
                    continue
                else:
                    pass

                list_far.append(far)
                list_end.append(end)
                counter +=1

    return mask,counter,hull1,(cx,cy),list_far,list_end
opencv_button.py 文件源码 项目:OpenCV2 作者: SarathM1 项目源码 文件源码 阅读 14 收藏 0 点赞 0 评论 0
def main():
    while True:
        ret,img=cap.read()
        img = cv2.resize(img,None,fx=1.3,fy=1.2,interpolation = cv2.INTER_LINEAR)

        btn1 = img[0:100,250:350]
        btn1 = cv2.cvtColor(btn1,cv2.COLOR_BGR2GRAY)
        ret,mask = cv2.threshold(btn1,150,255,cv2.THRESH_BINARY_INV)
        (cnts,_)=cv2.findContours(mask.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)    

        res = cv2.bitwise_and(btn1,btn1,mask=mask)

        (cnts,_)=cv2.findContours(mask.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
        ci = 0
        max_area = 0
        if cnts:
            for i in range(len(cnts)):
                cnt=cnts[i]
                area = cv2.contourArea(cnt)
                if(area>max_area):
                    max_area=area
                    ci=i
            cnt = cnts[ci]
        else:
            cnt = None
        #cv2.drawContours(btn1,cnt,-1,(0,255,0),1)
        font = cv2.FONT_HERSHEY_SIMPLEX

        if cnt is not None:
            cv2.rectangle(img,(250,0),(350,100),(0,0,0),2)
            hull = cv2.convexHull(cnt)
            cv2.drawContours(btn1,[hull],0,(0,0,255),2)
            cv2.putText(img,"Btn1",(0,50), font, 1,(255,0,0),2,1)
        else:
            cv2.rectangle(img,(250,0),(350,100),(188,188,137),2)

        cv2.imshow('Img',img)
        #cv2.imshow('btn1',mask)


        if cv2.waitKey(20)&0xff==ord('q'):
            cv2.imwrite('btn1.jpg',btn1)
            cv2.imwrite('mask.jpg',mask)
            break

    cap.release()
    cv2.destroyAllWindows()
Segmenter.py 文件源码 项目:ImageSegLoaderPart 作者: Layman806 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def segment(self):
        self.im_gray = cv2.medianBlur(self.im_gray, 5)
        # Apply adaptive threshold with binary_inv
        thresh = cv2.adaptiveThreshold(self.im_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
        # apply some dilation and erosion to join the gaps
        thresh = cv2.dilate(thresh, None, iterations=3)
        thresh = cv2.erode(thresh, None, iterations=2)
        # finding contours
        im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        '''
        cropped is a dictionary with (cx, cy) centroid tuples as keys, and cropped images as values
        centroids is a list of the same centroid tuples, (cx, cy)
            - This was done because it was not possible to sort the dictionary directly using tuples as keys using the sort(dict)
              function.
            - Instead, (cx, cy) was stored in the centroids list, and the list in turn was sorted using centroids.sort().
            - The list is then iterated upon to get tuples in order...
            - Each tuple iterated upon acts as a key for the dictionary, fetching the cropped images in order

        '''
        cropped = {(0, 0): '0'}
        centroids = [(0, 0)]

        for cnt in contours:
            x, y, w, h = cv2.boundingRect(cnt)
            # finding centroid coordinates, so that it can be the basis of sorting cropped images
            M = cv2.moments(cnt)
            cx = int(M['m10'] / M['m00'])
            cy = int(M['m01'] / M['m00'])
            # storing centroid tuple and cropped image in dictionary
            cropped[(cx, cy)] = self.im_gray[y:y + h, x:x + w]
            # inserting centroid tuples to a list
            centroids.append((cx, cy))

        # since (0, 0) was only a placeholder
        del cropped[(0, 0)]
        centroids.remove((0, 0))

        # sorting the centroid list
        centroids.sort()

        segments = []
        for c in centroids:
            segments.append(cropped[c])
        return segments
Segmenter.py 文件源码 项目:ImageSegLoaderPart 作者: Layman806 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def segment(self):
        self.im_gray = cv2.medianBlur(self.im_gray, 5)
        # Apply adaptive threshold with binary_inv
        thresh = cv2.adaptiveThreshold(self.im_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
        # apply some dilation and erosion to join the gaps
        thresh = cv2.dilate(thresh, None, iterations=3)
        thresh = cv2.erode(thresh, None, iterations=2)
        # finding contours
        im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        '''
        cropped is a dictionary with (cx, cy) centroid tuples as keys, and cropped images as values
        centroids is a list of the same centroid tuples, (cx, cy)
            - This was done because it was not possible to sort the dictionary directly using tuples as keys using the sort(dict)
              function.
            - Instead, (cx, cy) was stored in the centroids list, and the list in turn was sorted using centroids.sort().
            - The list is then iterated upon to get tuples in order...
            - Each tuple iterated upon acts as a key for the dictionary, fetching the cropped images in order

        '''
        cropped = {(0, 0): '0'}
        centroids = [(0, 0)]

        for cnt in contours:
            x, y, w, h = cv2.boundingRect(cnt)
            # finding centroid coordinates, so that it can be the basis of sorting cropped images
            M = cv2.moments(cnt)
            cx = int(M['m10'] / M['m00'])
            cy = int(M['m01'] / M['m00'])
            # storing centroid tuple and cropped image in dictionary
            cropped[(cx, cy)] = self.im_gray[y:y + h, x:x + w]
            # inserting centroid tuples to a list
            centroids.append((cx, cy))

        # since (0, 0) was only a placeholder
        del cropped[(0, 0)]
        centroids.remove((0, 0))

        # sorting the centroid list
        centroids.sort()

        segments = []
        for c in centroids:
            segments.append(cropped[c])
        return segments
fish_guild.py 文件源码 项目:osrmacro 作者: jjvilm 项目源码 文件源码 阅读 46 收藏 0 点赞 0 评论 0
def findCageOption(self):
        """Finds cage option in fishing guild when right clicked on fish bubbles"""
        x1 = 5
        y1 = 25
        x2 = 767
        y2 = 524
        rs_window = Screenshot.shoot(x1,y1,x2,y2)
        #cv2.imshow('img',rs_window)
        #cv2.waitKey(0)
        rsc = rs_window.copy()
        # gets only all the black and white
        ret,thresh1 = cv2.threshold(rsc,0,255,cv2.THRESH_BINARY)
        # inverst to only get black cloros as white
        ret,thresh1 = cv2.threshold(thresh1,0,255,cv2.THRESH_BINARY_INV)

        _, contours,h = cv2.findContours(thresh1,1,2)

        for cnt in contours:
            # looks for biggest square
            if cv2.contourArea(cnt) <= 1695.0:
                continue
            # checks contour sides
            approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
            # draws only if its squared
            if len(approx)==4:
                print("square of {}".format(cv2.contourArea(cnt)))
                #cv2.drawContours(rs_window,[cnt],0,(255,255,255),-1)
                # get geometry of approx
                # add rs coords
                x,y,w,h = cv2.boundingRect(cnt)

                #combines rs_window coords
                x += x1
                y += y1

                # scrshot of option menu on play window
                img = Screenshot.shoot(x,y,x+w,y+h)
                ret,thresh1 = cv2.threshold(img,254,255,cv2.THRESH_BINARY)

                # loads image from db
                img_from_dict = self.idb.pickled_dict['cage']


                #finds a match 
                from modules import Match
                # runs func when match is found returns true to keep looking for template
                if Match.images(thresh1,img_from_dict,x,y, self.doInMatched):
                    # keep looking for other bubbles
                    return 1
                else:
                    # found 'cage'
                    return 0
        # in case the options menu is aginast an edge
        return 1
line_detector.py 文件源码 项目:line-follower 作者: iseikr 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def __findLine(self):
        self.__grabImage();

        if(self.currentImage is None):
            #grabbing image failed
            return -2.0

        #Convert to Grayscale
        img = cv2.cvtColor(self.currentImage, cv2.COLOR_BGR2GRAY)

        #Blur to reduce noise
        img = cv2.medianBlur(img,25)

        #Do Thresholding
        h,img = cv2.threshold(img, self.thresh, self.maxValue, cv2.THRESH_BINARY_INV)

        img = cv2.blur(img,(2,2))

        #Make image smaller
        img = cv2.resize(img, (self.horizontalRes, self.verticalRes))
        #org_img = cv2.resize(org_img, (self.horizontalRes, self.verticalRes))

        #Create skeleton
        size = np.size(img)
        skel = np.zeros(img.shape,np.uint8)
        element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
        done = False
        while( not done):
            eroded = cv2.erode(img,element)
            temp = cv2.dilate(eroded,element)
            temp = cv2.subtract(img,temp)
            skel = cv2.bitwise_or(skel,temp)
            img = eroded.copy()
            zeros = size - cv2.countNonZero(img)
            if zeros==size:
                done = True

        #Do Line Detection
        lines = cv2.HoughLinesP(skel,1,np.pi/180,2,
                self.hough_minLineLength,self.hough_maxLineGap)


        #get minimum and maximum x-coordinate from lines
        x_min = self.horizontalRes+1.0
        x_max = -1.0;
    if(lines != None and len(lines[0]) > 0):
        for x1,y1,x2,y2 in lines[0]:
            x_min = min(x_min, x1, x2)
            x_max = max(x_max, x1, x2)
            #cv2.line(org_img,(x1,y1),(x2,y2),(0,255,0),2)

        #write output visualization
        #cv2.imwrite("output-img.png",org_img);

        #find the middle point x of the line and return
        #return -1.0 if no lines found
        if(x_max == -1.0 or x_min == (self.horizontalRes+1.0) ):
            return -1.0 #no line found
        else:
            return (x_min + x_max) / 2.0
trackgesture.py 文件源码 项目:CNNGestureRecognizer 作者: asingh33 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def binaryMask(frame, x0, y0, width, height ):
    global guessGesture, visualize, mod, lastgesture, saveImg

    cv2.rectangle(frame, (x0,y0),(x0+width,y0+height),(0,255,0),1)
    roi = frame[y0:y0+height, x0:x0+width]

    gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray,(5,5),2)
    #blur = cv2.bilateralFilter(roi,9,75,75)

    th3 = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,2)
    ret, res = cv2.threshold(th3, minValue, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    #ret, res = cv2.threshold(blur, minValue, 255, cv2.THRESH_BINARY +cv2.THRESH_OTSU)

    if saveImg == True:
        saveROIImg(res)
    elif guessGesture == True:
        retgesture = myNN.guessGesture(mod, res)
        if lastgesture != retgesture :
            lastgesture = retgesture
            #print lastgesture

            ## Checking for only PUNCH gesture here
            ## Run this app in Prediction Mode and keep Chrome browser on focus with Internet Off
            ## And have fun :) with Dino
            if lastgesture == 3:
                jump = ''' osascript -e 'tell application "System Events" to key code 49' '''
                #jump = ''' osascript -e 'tell application "System Events" to key down (49)' '''
                os.system(jump)
                print myNN.output[lastgesture] + "= Dino JUMP!"

            #time.sleep(0.01 )
            #guessGesture = False
    elif visualize == True:
        layer = int(raw_input("Enter which layer to visualize "))
        cv2.waitKey(1)
        myNN.visualizeLayers(mod, res, layer)
        visualize = False

    return res

#%%


问题


面经


文章

微信
公众号

扫码关注公众号