示例#1
0
def affine_transform(img):
    #print profile_path, name
    rows, cols, ch = img.shape

    x_rot = 50
    y_rot = 200

    pts1 = np.float32([[50, 50], [200, 50], [50, 200]])

    x_rot = x_rot + 20
    y_rot = y_rot + 20

    pts2 = np.float32([[x_rot, x_rot], [200, 50], [x_rot, y_rot]])

    M = cv2.getAffineTransform(pts1, pts2)

    dst = cv2.warpAffine(img, M, (cols, rows))
    salt = saltandpepper1.noise_addition(dst)
    #cv2.imshow('salt', salt)
    #cv2.waitKey()
    con_img = convolve_image(salt)
    #cv2.imshow('consalt', con_img)
    #cv2.waitKey()
    return con_img
示例#2
0
def generate_affine(img, name):
    #for i in range(10):
    for i in range(30):

        #padd the image
        img1 = padd(img)
        #cv2.imshow('after concatat',img1)
        #cv2.waitKey()
        gamma_1 = random_1()
        gamma_main = random.randrange(1, 3, 1)
        gamma_last = gamma_main + gamma_1
        noise_addition = adjust_gamma(
            img1, gamma=gamma_last)  #change the gammmaa of the image randomly
        noise_ad2 = sp.noise_addition(
            noise_addition)  #add the salt and peppr noise to the image
        pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
        (h, w) = img1.shape[:2]
        rows2, cols2, ch2 = img1.shape  #ch=3 rows = hieght and col is width

        #creating the rotation matrix
        x = random.randrange(-3, 3, 1)

        y = random.random()
        z = x + y
        x = math.radians(z)

        sin = math.sin(x)
        cos = math.cos(x)
        rotation = [[cos, sin], [-sin, cos]]  #2x2 rotation matrix
        #shear matrices

        shear_fact_x = -0.05 + random_2()  #take random values for both x and y
        shear_fact_y = -0.05 + random_2()
        shear_x = [[1, shear_fact_x], [0, 1]]
        shear_y = [[1, 0], [shear_fact_y, 1]]
        shear_new = np.array(shear_x, dtype=np.float32)
        rotation_new = np.array(rotation, dtype=np.float32)

        #multiplying 2D matrices
        afine_t = np.dot(shear_new, rotation_new)
        shear_new = np.array(shear_y, dtype=np.float32)
        afine_t2 = np.dot(afine_t, shear_new)
        #afine_t = shear_new
        pts2 = np.dot(pts1, afine_t2)  #new points use for warping the image
        #print pts2
        #cv2.imshow('Affine_2',img)
        #cv2.waitKey()
        s11, s12, s13 = noise_ad2.shape
        for r in range(s11):
            for c in range(s12):
                if np.all(noise_ad2[r, c, :] == 0):

                    noise_ad2[r, c, 0] = noise_ad2[r, c, 0] + 1
                    noise_ad2[r, c, 1] = noise_ad2[r, c, 1] + 1
                    noise_ad2[r, c, 2] = noise_ad2[r, c, 2] + 1

        M = cv2.getAffineTransform(pts1, pts2)
        dst = cv2.warpAffine(noise_ad2, M, (cols2, rows2))  #get afine of image
        #cv2.imshow('imshow',dst)
        #cv2.imshow('Affine_3',dst)
        #cv2.waitKey()
        s1, s2, s3 = dst.shape
        #removing the extra black areas from the images so that only logos is visible.

        count = 0
        count2 = 0
        for r in range(s1):
            num = count_rows(dst[r, :, :])
            #print '*******NUM*******', num
            if num >= 30:
                count = count + 1
                #break
            else:
                #count = count+1
                break
        for c in range(s2):
            num2 = count_rows(dst[:, c, :])
            #print '*******NUM*******1\t', num2
            if (num2 >= 30):
                count2 = count2 + 1
                #break
            else:
                #count2 = count2+1
                break

        r1 = s1 - 1
        while (r1 >= 0):
            num3 = count_rows(dst[r1, :, :])
            #print '*******NUM*******2\t', num3
            if (num3 >= 30):
                #break
                r1 = r1 - 1
            else:
                #r1=r1-1
                break
        c1 = s2 - 1
        while (c1 >= 0):
            num4 = count_rows(dst[:, c1, :])
            #print '*******NUM*******3\t', num4
            if (num4 >= 30):
                #break
                c1 = c1 - 1
            else:
                #c1 = c1-1
                break
    # print 'first and  last also col1 and col2',r1,count,count2,c1

        dst2 = dst[count:r1, count2:c1]
        #major change
        or_image_x, or_image_y, or_image_z = img.shape
        #dst2 = dst
        s1, s2, s3 = dst2.shape
        t2 = (-or_image_y + s2) / 2
        t1 = (-or_image_x + s1) / 2
        #print '***t2,t2***', t1, t2

        count = r1 = c1 = count2 = 0

        #print '*******s1*******', s1, s2, s3
        if s1 > 100 and s2 > 100:
            dst2 = dst2[t1:s1 - t1, t2:s2 - t2]
            #cv2.imshow('Affine',dst2)
            #cv2.waitKey()
        else:
            #print '******AFFINE FAILED********'
            dst2 = img  #comping original image to dst
        #add the gist and hog feature of the image
        if s2 != 0 and s1 != 0:

            #        cv2.imshow('noise',dst2)
            #dst2 remove black.
            gist_feat = Gist_feat_last.singleImage2(dst2)
            hog_feat = HOG_feat2.hog_call(dst2)
            gist_feat = np.concatenate((gist_feat, [1]))
            hog_feat = np.concatenate((hog_feat, [1]))
            '''add_feature_to_database(name,gist_feat,"Gabor")
            add_feature_to_database(name,hog_feat,"HOG")'''

            with open(feat_gist + name + '.csv', 'a') as myfile:
                wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
                wr.writerow(gist_feat)
            with open(feat_hog + name + '.csv', 'a') as myfile:
                wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
                wr.writerow(hog_feat)

    return i