Example #1
0
def deco_stretch(file, target_mean=None, target_sigma=None):
    data_input = file
    data_mean, data_std = cv2.meanStdDev(data_input)
    stretch = None
    pca_data = data_input.flatten()  #np.asarray(data_input).reshape(-1)
    print(pca_data)
    pca_obj, pca_eigen, eigen_values = cv2.PCACompute2(pca_data,
                                                       mean=target_mean)

    eig_data_sigma = np.sqrt(eigen_values)
    print(eig_data_sigma)
    scale = np.diag(1 / eig_data_sigma)
    print(scale)
    if target_sigma is None:
        stretch = np.diag(data_std)
    else:
        stretch = np.diag(target_sigma)
    stretch = np.float32(stretch)

    repmat_data = cv2.repeat(np.transpose(pca_obj), pca_data.shape[0], 1)
    zmat_data = cv2.subtract(pca_data, repmat_data, dtype=cv2.CV_32F)

    if target_mean is not None:
        repmat_data = cv2.repeat(np.transpose(target_mean), pca_data.shape[0],
                                 1)
    transformed = zmat_data * (np.transpose(pca_eigen) * scale * pca_eigen *
                               stretch)
    transformed = cv2.add(transformed, repmat_data, dtype=cv2.CV_32F)

    dstr32f = transformed.reshape(data_input.shape[0], -1, data_input.shape[2])

    return dstr32f
Example #2
0
def getBases(size, type=np.float64):
    B = np.ones((size * size, 6), dtype=type)
    row = np.ones((1, size), dtype=type)
    cols = np.ones((size, 1), dtype=type)
    for i in range(0, size):
        row[0, i] = i - np.floor(size / 2)
        cols[i, 0] = i - np.floor(size / 2)
    B[..., 1] = cv.repeat(row, size, 1).flatten()  #x
    B[..., 2] = cv.repeat(cols, 1, size).flatten()  #y
    B[..., 3] = B[..., 1] * B[..., 1]  #x^2
    B[..., 4] = B[..., 2] * B[..., 2]  #y^2
    B[..., 5] = B[..., 1] * B[..., 2]  #xy
    return B
Example #3
0
def dcs_transform(img, targetMean, targetSigma):

    # flatten to 2d
    data = img.reshape(-1, img.shape[-1])

    # compute mean and stddev of input
    dataMu, dataSigma = cv2.meanStdDev(img)

    # compute pca
    mean = np.empty((0))
    mean, eigenvectors, eigenvalues = cv2.PCACompute2(data, mean)

    # scaling matrix (Sc)
    eigDataSigma = np.sqrt(eigenvalues)
    scale = np.diagflat(1 / eigDataSigma)

    # stretching matrix (St)
    # if targetSigma is empty, set sigma of transformed data equal to that of original data
    if targetSigma is None:
        stretch = np.diagflat(dataSigma)
    else:
        stretch = np.diagflat(targetSigma)

    # stretching matrix (St)
    repMu = cv2.repeat(dataMu.T, data.shape[0], 1)
    zmudata = cv2.subtract(data.astype(float), repMu)

    if targetMean is not None:
        repMu = np.tile(targetMean.T, (data.shape[0], 1))

    # compute pca transformation
    transformed = zmudata @ (eigenvectors.T @ scale @ eigenvectors @ stretch)
    transformed = np.add(transformed, repMu)

    return transformed.reshape(img.shape)
Example #4
0
    def train_scale(self, image, ini=False):
        xsf = self.get_scale_sample(image)

        # Adjust ysf to the same size as xsf in the first time
        if ini:
            totalSize = xsf.shape[0]
            self.ysf = cv2.repeat(self.ysf, totalSize, 1)

        # Get new GF in the paper (delta A)
        new_sf_num = cv2.mulSpectrums(self.ysf, xsf, 0, conjB=True)

        new_sf_den = cv2.mulSpectrums(xsf, xsf, 0, conjB=True)
        new_sf_den = cv2.reduce(real(new_sf_den), 0, cv2.REDUCE_SUM)

        if ini:
            self.sf_den = new_sf_den
            self.sf_num = new_sf_num
        else:
            # Get new A and new B
            self.sf_den = cv2.addWeighted(self.sf_den, (1 - self.scale_lr),
                                          new_sf_den, self.scale_lr, 0)
            self.sf_num = cv2.addWeighted(self.sf_num, (1 - self.scale_lr),
                                          new_sf_num, self.scale_lr, 0)

        self.update_roi()
Example #5
0
 def saveBackgrounds(self, bgs):
     nbgs = [len(e['bgs']) for e in bgs]
     cols = max(nbgs)
     fullR = nbgs.index(cols)
     fullBgs, mSep = [bgs[fullR][k] for k in ['bgs', 'mean_separator']]
     bg = self.bgrImg(self.bg_imgs.center)
     bg[:, :, :] = 255
     h, w = bg.shape[:2]
     bg = cv2.repeat(bg, len(bgs), cols)
     for r, e in enumerate(bgs):
         ebgs = e['bgs']
         for c, bg1 in enumerate(ebgs):
             if r != fullR:
                 cD = c if len(ebgs) == len(
                     fullBgs) else num.mean(bg1) > mSep
                 bgD = num.absolute(bg1 - fullBgs[c])
                 rv, bg1 = cv2.threshold((bgD - bgD.min()) * 4, 255, 0,
                                         cv2.THRESH_TRUNC)
             x, y = c * w, r * h
             bg[y:y + h, x:x + w] = cv2.flip(self.bgrImg(bg1), 0)
             if c > 0:
                 bg[y:y + h, x] = 255
             if c + 1 == len(ebgs) and r > 0:
                 bg[y, 0:cols * w] = 255
     cv2.imwrite(self.get_filename_with_extension('_bg.png'), bg)
     with open(params.bgs_file, 'wb') as f:
         cPickle.dump(
             dict(recalc_n_frames=self.movie.recalc_n_frames(),
                  backgrounds=bgs), f, -1)
Example #6
0
def _add_high_iso_noise(x):
    noise = cv2.repeat(crop(noise_image, 300, 64), 15, 15)
    if x.shape[0] > noise.shape[0] or x.shape[1] > noise.shape[1]:
        raise Exception('Image too big for HIGH ISO noise')

    noise_patch = crop(noise, x.shape[0],
                       min(1, abs(noise.shape[0] // 2 - x.shape[0])))
    noise_patch = noise_patch.astype('float32') / (2 * 255)
    x = x[:, :, :3].astype('float32') / (255 * 2)
    res = np.array(x + noise_patch)
    #print(np.max(res))
    return res
Example #7
0
    def Run(self, x=0, image=False):
        # Calibration.
        self.MainCalibration()
        if x == 1:
            self.StepCalibration()
        elif x == 2:
            self.SwerveCalibration()

        # Main execution.

        if image:
            # Generates new window.
            cv2.namedWindow('Running')

        while True:
            # Captures a camera frame.
            self.capture()
            # Makes a image copy.
            if image:
                res = cv2.repeat(self.img, 1, 1)
            else:
                res = 0

            # Runs the main segmentation.
            M = self.MainRunning(res)
            if x == 1:
                # Runs the segmentation for the Step Challenge
                St = self.StepRunning(res)
            elif x == 2:
                # Runs the segmentation for the Swerve Challenge
                Sw = self.SwerveRunning(res)

            if image:
                # Shows image on screen.
                cv2.imshow('Running', cv2.resize(res, (int(len(res[0]) * self.scl), int(len(res) * self.scl))))

            print "Main:", M,
            if x == 1:
                print "- Step:", St
            elif x == 2:
                print "- Swerve:", Sw
            else:
                print

            # Wait 'q' to be pressed
            if cv2.waitKey(20) & 0xFF == ord('q'):
                break

        # Closes all windows.
        cv2.destroyAllWindows()
Example #8
0
    def wallpaperSizeFitting(self):

        self.wp_resized = np.zeros(self.room.shape, np.uint8)

        #crop the wallpaper to fit the dimensions of the image (if it is oversized)
        if self.wallpaper.shape[0] > self.room.shape[
                0] or self.wallpaper.shape[1] > self.room.shape[1]:
            self.wp_resized = self.wallpaper[0:self.room.shape[0],
                                             0:self.room.shape[1]]
        #iterates wallpaper to fill the dimensions of the image (if it is undersized)
        elif self.wallpaper.shape[0] < self.room.shape[
                0] or self.wallpaper.shape[1] < self.room.shape[1]:
            #another_wpmask = np.zeros((imgs_h,imgs_w),np.uint8)
            x_iter = (self.room.shape[0] // self.wallpaper.shape[0]) + 1
            y_iter = (self.room.shape[1] // self.wallpaper.shape[1]) + 1
            self.wp_resized = cv.repeat(self.wallpaper, x_iter, y_iter)
            self.wp_resized = self.wp_resized[0:self.room.shape[0],
                                              0:self.room.shape[1]]
def get_blobs_info(cv_image):
    """
    Gets information about the colored blobs in the image

    :param cv_image: The OpenCV image to get blobs from
    :return: A dictionary containing an array of points for each colored blob found, represented by its hue.
        For example, in an image with 2 red blobs and 3 blue blobs, it will return
            {0: [(639, 558), (702, 555)], 120: [(567, 550), (698, 515), (648, 515)]}
    """

    # Show original image
    #cv2.imshow("Original image", cv_image)

    # Apply blur
    BLUR_SIZE = 3
    cv_image_blur = cv2.GaussianBlur(cv_image, (BLUR_SIZE, BLUR_SIZE), 0)
    #cv2.imshow("Blur", cv_image_blur)

    # Apply ROI mask
    mask_path = rospkg.RosPack().get_path(
        'sorting_demo') + "/share/head-mask.png"
    cv_mask = cv2.imread(mask_path)
    cv_mask = cv2.cvtColor(cv_mask, cv2.COLOR_BGR2GRAY)

    cv_image_masked = cv2.bitwise_and(cv_image_blur,
                                      cv_image_blur,
                                      mask=cv_mask)
    #cv2.imshow("Masked original", cv_image_masked)

    # HSV split
    cv_image_hsv = cv2.cvtColor(cv_image_masked, cv2.COLOR_BGR2HSV)
    cv_image_h, cv_image_s, cv_image_v = cv2.split(cv_image_hsv)
    #cv2.imshow("Image H", cv_image_h)
    #cv2.imshow("Image S", cv_image_s)
    #cv2.imshow("Image V", cv_image_v)

    # Apply CLAHE to Value channel
    CLAHE_SIZE = 16
    clahe = cv2.createCLAHE(clipLimit=2.0,
                            tileGridSize=(CLAHE_SIZE, CLAHE_SIZE))
    cv_image_v_clahe = clahe.apply(cv_image_v)

    # Merge channels
    cv_image_clahe = cv2.merge((cv_image_h, cv_image_s, cv_image_v_clahe))
    #cv2.imshow("CLAHE", cv_image_clahe)

    # Multiply Saturation and Value channels to separate the cubes, removing the table
    cv_image_sv_multiplied = cv2.multiply(cv_image_s,
                                          cv_image_v_clahe,
                                          scale=1 / 255.0)
    #cv2.imshow("Image S*V", cv_image_sv_multiplied)

    # Binarize the result
    BIN_THRESHOLD = 127
    #ret, cv_image_binary = cv2.threshold(cv_image_sv_multiplied, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    ret, cv_image_binary = cv2.threshold(cv_image_sv_multiplied, BIN_THRESHOLD,
                                         255, cv2.THRESH_BINARY)
    #cv2.imshow("Threshold", cv_image_binary)

    # Calculate H-channel histogram, applying the binarized image as mask
    cv_image_h_histogram = cv2.calcHist([cv_image_h], [0], cv_image_binary,
                                        [256], [0, 255])

    # Smoothen the histogram to find local maxima
    HISTOGRAM_BLUR_SIZE = 11
    histogram_length = len(cv_image_h_histogram)
    cv_image_h_histogram_wrap = cv2.repeat(cv_image_h_histogram, 3, 1)
    cv_image_h_histogram_smooth = cv2.GaussianBlur(
        cv_image_h_histogram_wrap, (HISTOGRAM_BLUR_SIZE, HISTOGRAM_BLUR_SIZE),
        0)
    cv_image_h_histogram_cut = cv_image_h_histogram_smooth[histogram_length:2 *
                                                           histogram_length]

    # Collect high peaks
    PEAK_RATIO = 0.2
    histogram_peak_cut = PEAK_RATIO * max(cv_image_h_histogram_cut)
    histogram_peaks = [
        i for i in range(len(cv_image_h_histogram_cut))
        if cv_image_h_histogram_cut[(i - 1) % histogram_length] <
        cv_image_h_histogram_cut[i] > cv_image_h_histogram_cut[
            (i + 1) % histogram_length]
    ]
    histogram_high_peaks = filter(
        lambda x: cv_image_h_histogram_cut[x] > histogram_peak_cut,
        histogram_peaks)
    #print(histogram_high_peaks)
    #pyplot.plot(cv_image_h_histogram_cut)
    #pyplot.show()

    # Process every color found in the histogram
    blob_info = {}
    cv_image_contours_debug = cv2.cvtColor(cv_image_binary, cv2.COLOR_GRAY2BGR)
    for current_hue in histogram_high_peaks:
        # Perform a Hue rotation that will be used to make detecting the edge colors easier (red in HSV corresponds to both 0 and 180)
        HUE_AMPLITUDE = 5
        cv_image_h_rotated = cv_image_h.copy()
        cv_image_h_rotated[:] -= current_hue
        cv_image_h_rotated[:] += HUE_AMPLITUDE

        # Binarize using range function
        cv_image_h_inrange = cv2.inRange(cv_image_h_rotated, 0,
                                         HUE_AMPLITUDE * 2)

        # Apply binary mask (consider that both black and the edge color have hue 0)
        cv_image_h_masked = cv2.bitwise_and(cv_image_h_inrange,
                                            cv_image_h_inrange,
                                            mask=cv_image_binary)

        # Erode
        EROSION_SIZE = 5
        erosion_kernel = numpy.ones((EROSION_SIZE, EROSION_SIZE), numpy.uint8)
        cv_image_h_eroded = cv2.erode(cv_image_h_masked, erosion_kernel)
        #cv2.imshow("inRange {}".format(histogram_high_peaks.index(current_hue)), cv_image_h_eroded)

        # Find convex contours
        _, contours, _ = cv2.findContours(cv_image_h_eroded.copy(),
                                          cv2.RETR_LIST,
                                          cv2.CHAIN_APPROX_SIMPLE)
        convex_contours = [cv2.convexHull(cnt) for cnt in contours]

        contour_color_hsv = numpy.array([[[current_hue, 255, 255]]],
                                        numpy.uint8)
        contour_color_rgb = cv2.cvtColor(contour_color_hsv,
                                         cv2.COLOR_HSV2BGR)[0][0].tolist()
        #cv2.drawContours(cv_image_contours_debug, convex_contours, -1, contour_color_rgb, 1)

        # Find centroids
        contour_moments = [cv2.moments(cnt) for cnt in convex_contours]
        contour_centroids = [(int(moments["m10"] / moments["m00"]),
                              int(moments["m01"] / moments["m00"]))
                             for moments in contour_moments
                             if moments["m00"] != 0]
        for (cx, cy) in contour_centroids:
            cv2.circle(cv_image_contours_debug, (cx, cy), 3, contour_color_rgb,
                       -1)

        # Collect data
        blob_info[current_hue] = contour_centroids

    #cv2.imshow("Convex contours", cv_image_contours_debug)

    return blob_info
Example #10
0
import cv2

image = cv2.imread("images/flip_test.jpg", cv2.IMREAD_COLOR)
if image is None: raise Exception("영상 파일 읽기 오류 발생")  # 예외 처리

x_axis = cv2.flip(image, 0)  # x축 기준 상하 뒤집기
y_axis = cv2.flip(image, 1)  # y축 기준 좌우 뒤집기
xy_axis = cv2.flip(image, -1)
rep_image = cv2.repeat(image, 1, 2)  # 반복 복사
trans_image = cv2.transpose(image)  # 행렬 전치

## 각 행렬을 영상으로 표시
titles = ['images', 'x_axis', 'y_axis', 'xy_axis', 'rep_image', 'trans_image']
for title in titles:
    cv2.imshow(title, eval(title))  #eval => 문자열을 명령어로 만들어줌 . 즉 행렬 변수로 사용
cv2.waitKey(0)
def main():
    fernew = open('fer2013new.csv')
    fer = open('fer2013.csv')
    reader1 = csv.reader(fernew, delimiter=',', quotechar='|')
    reader2 = csv.reader(fer, delimiter=',', quotechar='|')
    global all2
    global diff

    train = []
    validate = []
    test = []
    tv = []
    total = []
    multitrain = []
    multivalidate = []
    multitest = []
    multitv = []
    multitotal = []

    for i, j in zip(reader1, reader2):
        all2 += 1
        print all2
        if i[0] == 'Usage':
            continue
        l = map(lambda x: int(x), i[2:])

        # single label
        l1 = np.argmax(np.array(l))
        l2 = int(j[0])
        if l1 != fermap[l2]:
            diff += 1
        if l1 > 7:
            l1 = fermap[l2]

        # multi label
        # determine face's existence by people judger
        if human_judger and l[-1] >= 5:
            continue
        multilabel = l[:-2]
        multis = sum(multilabel)
        if multis == 0:
            continue
        multilabel = map(lambda x: 1.0 * x / multis, multilabel)

        data = np.array(map(lambda x: int(x), j[1].split(' ')), dtype=np.uint8)
        data.resize(48, 48)
        # 直方图均衡化,效果不明显
        # data = cv2.equalizeHist(data)

        gray_border = np.zeros((144, 144), np.uint8)
        cv2.repeat(data, 3, 3, gray_border)

        if i[0] == 'Training':
            D = range(0, 21, 5)
            D.extend(range(355, 339, -5))
            for d in D:
                ret, line = writeOne(gray_border, l1, d)
                if ret:
                    train.append(line)
                    tv.append(line)
                    total.append(line)
                    multitrain.append(multilabel)
                    multitv.append(multilabel)
                    multitotal.append(multilabel)
        else:
            ret, line = writeOne(gray_border, l1, 0)
            if ret:
                total.append(line)
                multitotal.append(multilabel)
                if i[0] == 'PublicTest':
                    validate.append(line)
                    tv.append(line)
                    multivalidate.append(multilabel)
                    multitv.append(multilabel)
                elif i[0] == 'PrivateTest':
                    test.append(line)
                    multitest.append(multilabel)
    fernew.close()
    fer.close()
    print 'different:', diff
    print 'image data written completed'

    shuffleData('train', train, multitrain)
    shuffleData('validate', validate, multivalidate)
    shuffleData('test', test, multitest)
    shuffleData('total', total, multitotal)
    shuffleData('tv', tv, multitv)
Example #12
0
def extendpat(pat,size):
    hr,wr = pat.shape
    wr = (size[0] - 1) // wr + 1
    hr = (size[1] - 1) // hr + 1
    return cv2.repeat(pat,hr,wr)[:size[1],:size[0]]
Example #13
0
if __name__ == '__main__':
    syms = utils.gensymbol(23,SYMLEN,SYMNUM)
    print(syms)

    pats = list(map(lambda x: extendpat(genpat(x),(1024,1024)),syms))
    cv2.imshow('dframe',pats[0].astype(np.uint8) + 128)
    cv2.waitKey(0)
    exit()

    #invc = cv2.VideoCapture('/home/user/Downloads/test.mkv')
    #invc = cv2.VideoCapture('/home/user/fin/MVI_9120.MOV')
    #for i in range(10):
    #    _,sframe = invc.read()
    #_,sframe = invc.read()
    sframe = cv2.imread('/home/user/Downloads/1908084_740428942732671_7684358515802468396_n.jpg')
    sframe = cv2.repeat(sframe,5,5)
    off = 0
    frame = sframe[:1024,off:1024 + off]
    
    #frame = cv2.cvtColor(frame,cv2.COLOR_RGB2HSV)
    #np.clip(frame[:,:,2] * 0.8 + 25.6 + (pats[0]),0,255,frame[:,:,2])
    #frame = cv2.cvtColor(frame.astype(np.uint8),cv2.COLOR_HSV2RGB)

    cv2.imshow('o',frame)
    #cv2.imwrite('out.png',frame[:284,:253])

    bri = cv2.cvtColor(frame,cv2.COLOR_RGB2HSV)[:,:,2]
    r,t = detch(bri)
    detpat(bri,r,t)

    '''
Example #14
0
# Creates a window to show the robots vision.
if args.show:
    cv2.namedWindow('Robot\'s Vision.')

S = (-1, -1, 0)
W = (-1, -1)
mS = 0

while True:
    # Capture a frame from the camera.
    main.capture()

    # Copies the frame.
    if args.show:
        res = cv2.repeat(main.img, 1, 1)
    else:
        res = None

    # Executes the functions.
    M = main.MainRunning(res)
    if args.step:
        S = main.StepRunning(res)
    if args.swerve:
        W = main.SwerveRunning(res)

    # Refreshes the window.
    if args.show:
        cv2.imshow(
            'Robot\'s Vision.',
            cv2.resize(
Example #15
0
def extendpat(pat, size):
    hr, wr = pat.shape
    wr = (size[0] - 1) // wr + 1
    hr = (size[1] - 1) // hr + 1
    return cv2.repeat(pat, hr, wr)[:size[1], :size[0]]
Example #16
0
def extendpat(pat,size):
    hr,wr = pat.shape
    wr = size[0] // wr
    hr = size[1] // hr
    return cv2.repeat(pat,hr,wr)
Example #17
0
def cv_backproject(src):
    ntimes=len(src)
    tmp_array=src/float(ntimes)
    return cv2.repeat(tmp_array,1,ntimes)
Example #18
0
import cv2

image = cv2.imread("dog.jpg", cv2.IMREAD_COLOR)
if image is None: raise Exception("사진 에러")
'''
cv2.flip(입력, 배열 뒤짚는 축) : 수직, 수평, 양축으로 뒤짚는다 각각 0, 1, -1

cv2.repeat(입력, 수직방향, 수평방향 반복횟수) : 입력 이미지를 반복하여 출력

cv2.transpose(입력) : 가로와 세로 방향을 바꿈
'''

x_axis = cv2.flip(image, 0)
y_axis = cv2.flip(image, 1)
xy_axis = cv2.flip(image, -1)
rep_img = cv2.repeat(image, 2, 2)
trans_img = cv2.transpose(image)

titles = ["image", "x_axis", "y_axis", "xy_axis", "rep_img", "trans_img"]
for title in titles:
    cv2.imshow(title, eval(title))

cv2.waitKey(0)
cv2.destroyAllWindows()
Example #19
0
def getPMatrix(mat):
    d = np.diag(mat).astype('float')

    rep = cv2.repeat(d, 1, len(d))
    return mat/rep
Example #20
0
    
def add_blue_hole(x):
    #xnew = x.copy()
    
    if len(x.shape) > 3:
        return np.array([_add_blue_hole(im) for im in x])
    else:
        return _add_blue_hole(x)

noise_image = np.ones((512, 512, 3))
try:
    noise_image = (plt.imread('../datasets/noise/101HPIMG/HPIM3024.JPG')[:,:,:3])*9
except FileNotFoundError:
    print('You do not have a noisy image sample')
    
noise = cv2.repeat(crop(noise_image, 300, 64), 15, 15)
def _add_high_iso_noise(x):
    if x.shape[0] > noise.shape[0] or x.shape[1] > noise.shape[1]:
        raise Exception('Image too big for HIGH ISO noise')
    
    return np.array(x + crop(noise, x.shape[0], min(1, abs(noise.shape[0]//2-x.shape[0]))))

def add_high_iso_noise(x):
    if len(x.shape) > 3:
        return np.array([_add_high_iso_noise(im) for im in x])
    else:
        return _add_high_iso_noise(x)
    
def _add_blur(x):
    kernel_blur = np.ones((3,3))/(3**2)
    return conv2d(x, kernel_blur)