Exemplo n.º 1
0
 def save(self, filename, cropFlag = False):
     """save cropped and rotated image"""
     if not cropFlag:
         cv.SaveImage(filename +'-vectors.png',self.render())
     else:
         tmp = rotate(self.cImage, self.center, self.cardinalOffset +\
                 self.north)
         #mask horizon
         mask = cv.CreateImage(self.res, 8, 1)
         cv.Zero(mask)
         cv.Circle(mask, self.center, self.radius, (255,255,255))
         cv.FloodFill(mask,(1,1),(0,0,0))
         cv.FloodFill(mask, self.center,
                 (255,255,255),lo_diff=cv.RealScalar(5))
         masked = cv.CloneImage(tmp)
         cv.Zero(masked)
         cv.Copy(tmp, masked, mask)
         cv.SaveImage(filename +'-cropped.png',crop(masked, self))
     #CSV output
     array = magnitudeToTheta(self.polarArray,self.radius)
     f = open(filename + '.csv', 'w')
     f.write('00\n')
     f.write(','.join([str(v[0]) for v in array]))
     f.write('\n')
     f.write(','.join([str(v[1]) for v in array]))
     f.write('\n')
     f.flush()
     f.close()
Exemplo n.º 2
0
def repaintCCs(image, doRepaint=None, returnMask=False, resizeMask=True, doFillBackground=True, bgPoint=(0, 0), newcol=255, connectivity=4):
    if doRepaint is None:
        doRepaint = lambda comp, col: False
    resultMask = cv.CreateImage((image.width + 2, image.height + 2), image.depth, image.nChannels)
    tempMask = cv.CreateImage((image.width + 2, image.height + 2), image.depth, image.nChannels)
    visitMask = cv.CreateImage((image.width + 2, image.height + 2), image.depth, image.nChannels)
    cv.Zero(resultMask)
    cv.Zero(tempMask)
    cv.Zero(visitMask)
    if doFillBackground:
        cv.FloodFill(image, bgPoint, 0, 0, 0, connectivity + cv.CV_FLOODFILL_MASK_ONLY + (255 << 8), visitMask)
    for x in xrange(image.width):
        for y in xrange(image.height):
            if visitMask[y + 1, x + 1] == 255:
                continue
            comp = cv.FloodFill(image, (x, y), 0, 0, 0, connectivity + cv.CV_FLOODFILL_MASK_ONLY + (255 << 8), tempMask)
            region = shiftRect(comp[2], 1, 1)
            cv.SetImageROI(tempMask, region)
            cv.SetImageROI(visitMask, region)
            cv.Or(tempMask, visitMask, visitMask)
            if doRepaint(comp, image[y, x]):
                cv.SetImageROI(resultMask, region)
                cv.Or(tempMask, resultMask, resultMask)
                cv.ResetImageROI(resultMask)
            cv.Zero(tempMask)
            cv.ResetImageROI(tempMask)
            cv.ResetImageROI(visitMask)
    if returnMask:
        if resizeMask: return cap.getSubImage(resultMask, (1, 1, image.width, image.height))
        else: return resultMask
    else:    
        cv.SetImageROI(resultMask, (1, 1, image.width, image.height))
        cv.Set(image, newcol, resultMask)
        return image
Exemplo n.º 3
0
def getNoiseMask(image, size, connectivity=8):
    mask = cv.CloneImage(image)
    for x in xrange(mask.width):
        for y in range(mask.height):
            col = mask[y, x]
            if col == black or col == white or col == 254:
                continue
            comp = cv.FloodFill(mask, (x, y), black, 0, 0, connectivity, None)
            if comp[0] > size:
                cv.FloodFill(mask, (x, y), 254, 0, 0, connectivity, None)
    cv.Threshold(mask, mask, 253, 255, cv.CV_THRESH_BINARY_INV)
    return mask
Exemplo n.º 4
0
def removeBadBackground(seg):
    threshUp = cv.CreateImage(cv.GetSize(seg), cv.IPL_DEPTH_8U, 1)
    comparison = cv.CreateImage(cv.GetSize(seg), cv.IPL_DEPTH_8U, 1)
    visitMask = cv.CreateImage(cv.GetSize(seg), cv.IPL_DEPTH_8U, 1)
    ffMask = cv.CreateImage((seg.width + 2, seg.height + 2), cv.IPL_DEPTH_8U,
                            1)
    cv.Threshold(seg, threshUp, 1, 255, cv.CV_THRESH_BINARY)
    cv.Zero(visitMask)
    cv.Zero(ffMask)
    for x in xrange(seg.width):
        for y in xrange(seg.height):
            if seg[y, x] != 96 or visitMask[y, x] == 255: continue
            comp = cv.FloodFill(threshUp, (x, y), 0, 0, 0,
                                4 + cv.CV_FLOODFILL_MASK_ONLY + (255 << 8),
                                ffMask)
            rect = comp[2]
            cv.SetImageROI(ffMask, cap.shiftRect(rect, 1, 1))
            cv.OrS(ffMask, 1, ffMask)
            cv.SetImageROI(seg, rect)
            cv.SetImageROI(comparison, rect)
            cv.Cmp(
                seg, ffMask, comparison,
                cv.CV_CMP_EQ)  # 'comparison' does not need to be zeroed later
            intersect = cv.CountNonZero(comparison)
            cv.SetImageROI(visitMask, rect)
            cv.Or(visitMask, ffMask, visitMask)
            cv.ResetImageROI(visitMask)
            if intersect == 0:
                cv.Set(seg, 0, ffMask)
            cv.Zero(ffMask)
            cv.ResetImageROI(seg)
            cv.ResetImageROI(ffMask)
    return seg
Exemplo n.º 5
0
def findCCs(image, erasecol=0, doContinue=None, doSkip=None, bRange=0, connectivity=8):
    """
    Finds all connected components in the image.
    doContinue is a function applied to the color of every new pixel in the image.
    If it is true, this pixel is ignored. Default: <= 128
    doSkip is a function applied to every new connected component found by the
    function. If it is true, this component will not be included in the result.
    Default: do not skip anything.
    """
    if doContinue is None:
        doContinue = lambda col: col <= 128
    if doSkip is None:
        doSkip = lambda comp: False
    mask = cv.CreateImage((image.width + 2, image.height + 2), cv.IPL_DEPTH_8U, 1)
    cv.Zero(mask)
    components = []
    for x in range(image.width):
        for y in range(image.height):
            if doContinue(image[y, x]):
                continue
            comp = cv.FloodFill(image, (x, y), 0, bRange, bRange, connectivity + cv.CV_FLOODFILL_MASK_ONLY + (255 << 8), mask) # here 3rd argument is ignored
            region = shiftRect(comp[2], 1, 1)
            if not doSkip(comp):
                seg = cvext.getSubImage(mask, region)
                components.append((comp[0], comp[1], comp[2], seg))
            cv.SetImageROI(image, comp[2])
            cv.SetImageROI(mask, region)
            cv.Set(image, erasecol, mask)
            cv.Zero(mask)
            cv.ResetImageROI(image)
            cv.ResetImageROI(mask)
    return components
Exemplo n.º 6
0
 def clearNoise(self, image):
     for x in range(image.width):
         for y in range(image.height):
             if image[y, x] == 0:
                 continue
             if image[y, x] < self.tLetter:
                 cv.FloodFill(image, (x, y), 0, self.rBright, self.rBright, 8, None)
     cv.Threshold(image, image, 1, 255, cv.CV_THRESH_BINARY_INV)
def on_mouse( event, x, y, flags, param ):

    if( not color_img ):
        return;

    if event == cv.CV_EVENT_LBUTTONDOWN:
            my_mask = None
            seed = (x,y);
            if ffill_case==0:
                lo = up = 0
                flags = connectivity + (new_mask_val << 8)
            else:
                lo = lo_diff;
                up = up_diff;
                flags = connectivity + (new_mask_val << 8) + cv.CV_FLOODFILL_FIXED_RANGE
            b = random.randint(0,255)
            g = random.randint(0,255)
            r = random.randint(0,255)

            if( is_mask ):
                my_mask = mask
                cv.Threshold( mask, mask, 1, 128, cv.CV_THRESH_BINARY );
               
            if( is_color ):
            
                color = cv.CV_RGB( r, g, b );
                comp = cv.FloodFill( color_img, seed, color, cv.CV_RGB( lo, lo, lo ),
                             cv.CV_RGB( up, up, up ), flags, my_mask );
                cv.ShowImage( "image", color_img );
            
            else:
            
                brightness = cv.RealScalar((r*2 + g*7 + b + 5)/10);
                comp = cv.FloodFill( gray_img, seed, brightness, cv.RealScalar(lo),
                             cv.RealScalar(up), flags, my_mask );
                cv.ShowImage( "image", gray_img );
            

            print "%g pixels were repainted" % comp[0]

            if( is_mask ):
                cv.ShowImage( "mask", mask );
Exemplo n.º 8
0
    def getContours(self):
        color = 100

        contours = []
        roi = []
        for w in range(self.width):
            for h in range(self.height):
                if self.temporary_img[h, w] == 255:
                    cc = cv.FloodFill(self.temporary_img, (w, h), color)
                    contours.append(cc[0])
                    roi.append(cc[2])

        return contours, roi
Exemplo n.º 9
0
 def doThis():
     result = cv.CloneImage(image)
     for x in xrange(result.width):
         for y in xrange(result.height):
             if result[y, x] == 0:
                 continue
             if result[y, x] < tLetter:
                 cv.FloodFill(result, (x, y), 0, rBright, rBright, 8, None)
                 if result[0, 0] != image[0, 0] or \
                    result[result.height - 1, 0] != image[image.height - 1, 0] or \
                    result[0, result.width - 1] != image[0, image.width - 1] or \
                    result[result.height - 1, result.width - 1] != image[image.height - 1, image.width - 1]:
                     return None
     return result
def split_captcha(filenameIN):
    threshold = 150
    threshold = 200
    maxValue = 255
    thresholdType = cv.CV_THRESH_BINARY
    srcImg = cv.LoadImage(filenameIN, 1)
    grayThresh = cv.CreateImage((srcImg.width, srcImg.height), cv.IPL_DEPTH_8U,
                                1)
    cv.CvtColor(srcImg, grayThresh, cv.CV_BGR2GRAY)
    cv.Threshold(grayThresh, grayThresh, threshold, maxValue, thresholdType)
    cv.SaveImage((filenameIN + "grayThresh.bmp"), grayThresh)
    connectivity = 4
    CCs4 = []

    gray4 = cv.CloneImage(grayThresh)

    for i in range(gray4.width):
        for j in range(gray4.height):
            if (cv.Get2D(gray4, j, i)[0] == 0):
                cc = CC()
                cc.mask = cv.CreateImage((gray4.width + 2, gray4.height + 2),
                                         cv.IPL_DEPTH_8U, 1)
                cv.Zero(cc.mask)
                cc.comp = cv.FloodFill(gray4, (i, j), cv.Scalar(128),
                                       cv.ScalarAll(0), cv.ScalarAll(0),
                                       connectivity, cc.mask)
                CCs4.append(cc)

    CCs4.sort(cmp=func_compare_area_cc)

    size = len(CCs4)
    for i in range(size):
        if (CCs4[size - 1 - i].comp[0] < 20):
            CCs4.pop()

    connectivity = 8
    CCs8 = []
    gray8 = cv.CloneImage(grayThresh)
    for i in range(gray8.width):
        for j in range(gray8.height):
            if (cv.Get2D(gray8, j, i)[0] == 0):
                cc = CC()
                cc.mask = cv.CreateImage((gray8.width + 2, gray8.height + 2),
                                         cv.IPL_DEPTH_8U, 1)
                cv.Zero(cc.mask)
                cc.comp = cv.FloodFill(gray8, (i, j), cv.Scalar(128),
                                       cv.ScalarAll(0), cv.ScalarAll(0),
                                       connectivity, cc.mask)
                CCs8.append(cc)
    CCs8.sort(cmp=func_compare_area_cc)

    size = len(CCs8)
    for i in range(size):
        if (CCs8[size - 1 - i].comp[0] < 20):
            CCs8.pop()

    CCs = []
    CCs = copy.copy(CCs8)
    # if (len(CCs8) < 3):
    #     CCs = copy.copy(CCs4)
    # else :
    #     if (CCs4[2].comp[0] < 20):
    #         CCs = copy.copy(CCs8)
    #     else:
    #         CCs = copy.copy(CCs4)
    CCs.sort(cmp=func_compare_pos_cc)
    letters = []
    letters_path = []

    for i in range(len(CCs)):
        letter = cv.CreateImage((WIDTH, HEIGHT), cv.IPL_DEPTH_8U, 1)
        cv.Set(letter, 255)
        letters.append(letter)
    for index_image in range(len(letters)):
        letter = letters[index_image]
        cc = CCs[index_image]

        offsetx = (WIDTH - cc.comp[2][2]) / 2
        offsety = (HEIGHT - cc.comp[2][3]) / 2

        for i in range(1, cc.mask.width - 1):
            for j in range(1, cc.mask.height - 1):
                if (cv.Get2D(cc.mask, j, i)[0] == 1):
                    Y = j - cc.comp[2][1] + offsety
                    X = i - cc.comp[2][0] + offsetx

                    if ((X > 0) and (X < WIDTH) and (Y > 0) and (Y < HEIGHT)):
                        cv.Set2D(letter, j - cc.comp[2][1] + offsety,
                                 i - cc.comp[2][0] + offsetx, cv.Scalar(0))
        letters_path.append(filenameIN + str(index_image + 1) + ".bmp")
        cv.SaveImage((filenameIN + str(index_image + 1) + ".bmp"),
                     letters[index_image])
        process_file(letters_path[index_image], WIDTH=31, HEIGHT=31)
    return letters_path
Exemplo n.º 11
0
cv.NamedWindow("test1", cv.CV_WINDOW_AUTOSIZE)
cv.NamedWindow("test2", cv.CV_WINDOW_AUTOSIZE)
#cv.NamedWindow("test3", cv.CV_WINDOW_AUTOSIZE)
#cv.NamedWindow("test4", cv.CV_WINDOW_AUTOSIZE)
#cv.NamedWindow("test5", cv.CV_WINDOW_AUTOSIZE)

cv.CvtColor(img, gray, cv.CV_RGB2GRAY)
cv.Not(gray, gray)
cv.Threshold(gray, binary, 20, 255, cv.CV_THRESH_BINARY)

color = 100
color_list = []
for x in range(binary.width):
    for y in range(binary.height):
        if (binary[y, x] > color):
            cv.FloodFill(binary, (x, y), color, cv.ScalarAll(10),
                         cv.ScalarAll(10), 0, msk)
            color_list.append(color)
            color = color + 1

obj = []
l_x = 0
r_x = 0
d_y = 0
u_y = 0
color = 0
for col in range(len(color_list)):
    for x in range(binary.width):
        for y in range(binary.height):
            if (binary[y, x] == color_list[col] and binary[y, x] != color):
                obj.append([x, x, y, y])
                color = color_list[col]