Exemplo n.º 1
0
    def PopulateImages(self):

        """
        Populate necessary images for opencv to use
        :return: 0 if the video is done
        """
        if self.csvFlag:
            try:
                points = self.PointsFromCSV()
            except:
                return 0
            self.img0 = self.PointsForJointsCSV(points) # img0 is a list of lists of points. each sublist corresponds to a joint. The last sublist corresponds to roguepoints
        else:
            ret, frame = self.cam.read()
            if frame is None:
                return 0# video done streaming
            if self.rotation != 0:
                if self.rotation == 90:
                    frame = cv2.flip(cv2.transpose(frame), 1)
                elif self.rotation  == -90:
                    frame = cv2.flip(cv2.transpose(frame), 0)
                elif abs(self.rotation) == 180:
                    frame = cv2.flip(cv2.flip(frame,0), 1)
                else:
                    raise Exception("Improper rotation value passed.")
            if prm.DEBUG:
                self.vis = frame.copy()
            self.frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            return 1#Good flag
Exemplo n.º 2
0
	def rotate_imgs(self):
		'''
			Rotates left and right images to proper orientation. Note this is done with transpose and flip
			as opposed to cv2.warpAffine to improve speed.
		''' 
		self.rotated_image_left = cv2.flip(cv2.transpose(self.raw_image_left), 0)
		self.rotated_image_right = cv2.flip(cv2.transpose(self.raw_image_right), 1)
Exemplo n.º 3
0
    def update_pos_stats(self, cam_matrix, distortion_coefficients, object_points):
        """
        Takes the square's corners found in the image, corresponding 3d
        coordinates, and intrinsic camera information. Sets fields related to
        extrinsic camera information: cam_rot, normal, cam_pos, location.
        Note that the square might be bogus until you check its score, and
        camera extrinsics are unaligned until align squares is called.
        :param cam_matrix:
        :param distortion_coefficients:
        :param object_points:
        :return: nothing
        """

        temp_corners = self.corners.reshape(4, 2, 1).astype(float)

        # Gets vector from camera to center of square
        inliers, self.rvec, self.tvec = cv2.solvePnP(object_points, temp_corners,
                                                     cam_matrix, distortion_coefficients)
        rot_matrix = cv2.Rodrigues(self.rvec)
        cam_pos = np.multiply(cv2.transpose(rot_matrix[0]), -1).dot(self.tvec)
        cam_to_grid_transform = np.concatenate((cv2.transpose(rot_matrix[0]), cam_pos), axis=1)
        grid_to_cam_transform = np.linalg.inv(np.concatenate((cam_to_grid_transform, np.array([[0, 0, 0, 1]])), axis=0))

        self.cam_rot = list(cam_to_grid_transform.dot(np.array([0, 0, 1, 0])))
        self.normal = grid_to_cam_transform.dot(np.array([0, 0, 1, 0]))
        self.cam_pos = cam_pos
        self.location = [cam_pos[0][0],cam_pos[1][0],cam_pos[2][0]]
def grabFrames():
	global imageLeft
	global imageRight

	# global filterStackLeft
	# global filterStackRight

	returnLeft, tempLeft = captureLeft.read()
	returnLeft, tempRight = captureRight.read()


	imageLeft = cv2.flip(cv2.transpose(tempLeft), 0) #imageLeft = cv2.flip(tempLeft, -1)
	imageRight = cv2.flip(cv2.transpose(tempRight), 0) #imageRight = tempRight#

	# grayL = cv2.cvtColor(imageLeft, cv2.COLOR_RGBA2GRAY)
	# grayR = cv2.cvtColor(imageRight, cv2.COLOR_RGBA2GRAY)


	# filterStackLeft = np.roll(filterStackLeft, 1, axis=0)
	# filterStackLeft[0] = grayL

	# filterStackRight = np.roll(filterStackRight, 1, axis=0)
	# filterStackRight[0] = grayR

	imageLeft = bilateralFilter(imageLeft)
	imageRight = bilateralFilter(imageRight)
Exemplo n.º 5
0
def getGradientInfo(img):
	sobelHorizontal = cv2.Sobel(img, ddepth = cv2.cv.CV_32F, dx = 1, dy = 0, ksize = 3)
	sobelVertical = cv2.Sobel(img, ddepth = cv2.cv.CV_32F, dx = 0, dy = 1, ksize = 3)

	magnitude, angle = cv2.cartToPolar(sobelHorizontal, sobelVertical)

	# sobelHorizontal = cv2.convertScaleAbs(sobelHorizontal)
	# sobelVertical = cv2.convertScaleAbs(sobelVertical)

	# sobelHorizontal2 = cv2.convertScale(sobelHorizontal)
	# sobelVertical2 = cv2.convertScale(sobelVertical)

	# cv2.imshow("Threshold", cv2.addWeighted(sobelHorizontal, 0.5, sobelVertical, 0.5, 0))

	# Quiver Plot
	res = 10
	N, M = img.shape
	X, Y = np.meshgrid(np.arange(0, M, res), np.arange(0, N, res))

	f = figure(1)
	imshow(img, cmap=cm.gray)
	# quiver(X, Y, sobelHorizontal[::res,::res], sobelVertical[::res,::res], units = "xy")
	abc = cv2.addWeighted(sobelHorizontal, 0.5, sobelVertical, 0.5, 0)

	print(abc.shape)
	quiver(X, Y, cv2.transpose(sobelHorizontal)[::res,::res], cv2.transpose(sobelVertical)[::res,::res])
	f.show()

	return 
Exemplo n.º 6
0
def cv2rotateimage(image, angle):
  """Efficient rotation if 90 degrees rotations, slow otherwise.

  Not a tensorflow function, using cv2 and scipy on numpy arrays.

  Args:
    image: a numpy array with shape [height, width, channels].
    angle: the rotation angle in degrees in the range [-180, 180].
  Returns:
    The rotated image.
  """
  # Limit angle to [-180, 180] degrees.
  assert angle <= 180 and angle >= -180
  if angle == 0:
    return image
  # Efficient rotations.
  if angle == -90:
    image = cv2.transpose(image)
    image = cv2.flip(image, 0)
  elif angle == 90:
    image = cv2.transpose(image)
    image = cv2.flip(image, 1)
  elif angle == 180 or angle == -180:
    image = cv2.flip(image, 0)
    image = cv2.flip(image, 1)
  else:  # Slow rotation.
    image = ndimage.interpolation.rotate(image, 270)
  return image
Exemplo n.º 7
0
def fix_orient(image,value):
	
	if value <= 1:
		#do nothing
		out = image.copy()
	elif value == 2:
		#flip image horizontally
		out = cv2.flip(image,1)
	elif value == 3:
		#flip vertically, horizontally or rotate 180
		out = cv2.flip(image,-1)
	elif value == 4:
		#flip vertically
		out = cv2.flip(image,0)
	elif value == 5:
		# transpose
		out = cv2.transpose(image)
	elif value == 6:
		# flip vertically, transpose or rotate 90
		temp = cv2.flip(image,0)
		out = cv2.transpose(temp)
	elif value == 7:
		# flip horizontally, vertically, transpose or transverse
		temp = cv2.flip(image,-1)
		out = cv2.transpose(temp)
	elif value == 8:
		# flip horizontally, transpose or rotate 270
		temp = cv2.flip(image,1)
		out = cv2.transpose(temp)   
	return out
Exemplo n.º 8
0
def rotate(input_file,output_file,mode) :
	img = cv2.imread(input_file,cv2.IMREAD_GRAYSCALE)
	rows,cols= img.shape
	temp = np.zeros((cols,rows),np.uint8)
	cv2.transpose(img,temp)
	cv2.flip(temp,mode,temp)	
	cv2.imwrite(output_file,temp)
Exemplo n.º 9
0
    def minervini(self, norm):
        # Calculate texture response filter from Minervini 2013
        # FIXME: radius, gaussian size and sigmas should be user defined.
        falloff = 1.0 / 50.0
        pillsize = 7
        gaussize = 17
        sdH = 4
        sdL = 1

        # pillbox feature (F1)
        pillse = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,
                                           (pillsize, pillsize))
        pillse = pillse.astype(float)
        pillse = pillse / sum(sum(pillse))
        F1 = cv2.filter2D(self.imgLAB[:, :, 1], -1, pillse)

        # Difference of Gaussian (DoG) featrue (F2)
        G1 = cv2.getGaussianKernel(gaussize, sdH)
        G2 = cv2.getGaussianKernel(gaussize, sdL)
        G1 = G1 * cv2.transpose(G1)
        G2 = G2 * cv2.transpose(G2)
        F2 = cv2.filter2D(self.imgLAB[:, :, 0], -1, G1 - G2)

        F = np.exp(-falloff * np.abs(F1 + F2))
        # FIXME: We are ignoring norm for now.
        F = self.normRange(F)
        F = np.reshape(F, (F.shape[0], F.shape[1], 1))

        return F
Exemplo n.º 10
0
def get_difference_y_image(image):
    kernel = numpy.array([[.5],[0],[-.5]])
    
    result = cv2.transpose(image.copy())
    cv2.filter2D(cv2.transpose(image.copy()), -1, kernel, result)
    
    return cv2.transpose(result)
Exemplo n.º 11
0
    def rotate(self, degrees):
        # see http://stackoverflow.com/a/23990392
        if degrees == 90:
            self.image = cv2.transpose(self.image)
            cv2.flip(self.image, 0, self.image)
        elif degrees == 180:
            cv2.flip(self.image, -1, self.image)
        elif degrees == 270:
            self.image = cv2.transpose(self.image)
            cv2.flip(self.image, 1, self.image)
        else:
            # see http://stackoverflow.com/a/37347070
            # one pixel glitch seems to happen with 90/180/270
            # degrees pictures in this algorithm if you check
            # the typical github.com/recurser/exif-orientation-examples
            # but the above transpose/flip algorithm is working fine
            # for those cases already
            width, height = self.size
            image_center = (width / 2, height / 2)
            rot_mat = cv2.getRotationMatrix2D(image_center, degrees, 1.0)

            abs_cos = abs(rot_mat[0, 0])
            abs_sin = abs(rot_mat[0, 1])
            bound_w = int((height * abs_sin) + (width * abs_cos))
            bound_h = int((height * abs_cos) + (width * abs_sin))

            rot_mat[0, 2] += ((bound_w / 2) - image_center[0])
            rot_mat[1, 2] += ((bound_h / 2) - image_center[1])

            self.image = cv2.warpAffine(self.image, rot_mat, (bound_w, bound_h))
Exemplo n.º 12
0
	def rotate90(self, image):
		if self.angle == 1:	# 90 deg
			return cv.transpose(cv.flip(image,1))
		elif self.angle == 2: # 180 deg
			return cv.flip(image,-1)
		elif self.angle == 3: # 270 deg
			return cv.flip(cv.transpose(image),1)
		else:
			return image
Exemplo n.º 13
0
def str2img(jpgstr, orientation=None):
    import numpy as np
    import cv2
    arr = np.fromstring(jpgstr, np.uint8)
    img = cv2.imdecode(arr, cv2.IMREAD_COLOR)
    if orientation == 1:
        return cv2.flip(cv2.transpose(img), 0) # counter-clockwise
    if orientation == 3:
        return cv2.flip(cv2.transpose(img), 1) # clockwise
    return img
Exemplo n.º 14
0
 def rotateImage(self, img, angle=90):
     """+-90 degree rotations are fast and do not crop"""
     if (angle == 90) :
         return(cv2.flip(cv2.transpose(img),flipCode=0))
     elif (angle == -90) :
         return(cv2.flip(cv2.transpose(img),flipCode=1))
     else :
         center = (img.shape[1]/2.0,img.shape[0]/2.0)
         rotate = cv2.getRotationMatrix2D(center, angle, 1.0)
         return cv2.warpAffine(img, rotate, (img.shape[1], img.shape[0]))
Exemplo n.º 15
0
def raycast(image, rays=4, resolution=1, ret_type="image", dist_delta=1, blur=False):
    """
    :param image: must be grayscale
    :param rays: number of rays to cast. must be 2, 4 or 8. 2 is left-right, 4 gives cardinal directions, 8 adds diagonal at 45 degrees.
    :param ret_type: "image" for all of the things composited, "array" for a list of separate frames
    :return: four or eight channel image of ray lengths, clockwise from up
    """
    if not ret_type in ["image", "array"]:
        print "invalid return type in raycast call"
        return
    init_im = image
    lr_ray_im = left_right_ray(image, resolution, dist_delta)
    image = cv2.flip(image, 1)
    rl_ray_im = cv2.flip(left_right_ray(image, resolution), 1, dist_delta)
    if rays in [2, 4, 8]:
        if ret_type == "image":
            ret = cv2.merge([lr_ray_im, rl_ray_im])
        elif ret_type == "array":
            ret = [lr_ray_im, rl_ray_im]

    if rays in [4, 8]:
        image = cv2.transpose(image)
        down_ray_im = cv2.transpose(left_right_ray(image, resolution, dist_delta), 1)
        image = cv2.flip(image, 1)
        up_ray_im = cv2.flip(cv2.transpose(cv2.flip(left_right_ray(image, resolution, dist_delta), 1)), 1)
        if ret_type == "image":
            ret = cv2.merge([up_ray_im, lr_ray_im, down_ray_im, rl_ray_im])
        elif ret_type == "array":
            ret = [up_ray_im, lr_ray_im, down_ray_im, rl_ray_im]

    if rays == 8:
        skew_1, deskew_1 = skew(init_im)
        sca_ne_sw = [deskew(x, reverse_affine=deskew_1) for x in raycast(skew_1, rays=2, ret_type="array", resolution=resolution, dist_delta=1.414)]
        skew_2, deskew_2 = skew(cv2.flip(init_im, 0))
        sca_se_nw = [cv2.flip(deskew(x, reverse_affine=deskew_2), 0) for x in raycast(skew_2, rays=2, ret_type="array", resolution=resolution, dist_delta=1.414)]
        array = [up_ray_im, sca_ne_sw[0], lr_ray_im, sca_se_nw[0], down_ray_im, sca_ne_sw[1], rl_ray_im, sca_se_nw[1]]
        if ret_type == "image":
            ret = cv2.merge(array)
        elif ret_type == "array":
            ret = array

    if blur:
        box_size = (resolution, resolution)
        if ret_type == "array":
            print "printing ret for investigative purposes:"
            print ret
            print "len(ret)", len(ret)
            print "ret[0]", type(ret[0]), ret[0]
            print "ret[1]", type(ret[1]), ret[1]

            ret = [cv2.boxFilter(x, -1, box_size, normalize=False) for x in ret]
        if ret_type == "image":
            ret = cv2.boxFilter(x, -1, box_size, normalize=False)
    return ret
Exemplo n.º 16
0
def RotateFrame(frame, theta_deg):
    """
    Rotate frame in multiples of 90 degrees.

    Arguments
    ----
    frame : numpy uint8 array
        Video frame to rotate.
    theta_deg : integer
        CCW rotation angle in degrees (math convention)
        Integer multiples of 90 degrees are handled quickly.
        Arbitrary rotation angles are slower.

    Returns
    ----
    new_frame : numpy uint8 array
        rotated frame

    Example
    ----
    >>> frame_rot = RotateFrame(frame, 90)
    """

    if theta_deg == 0:

        # Do nothing
        new_frame = frame.copy()

    elif theta_deg == 90:

        # Rotate CCW 90
        new_frame = cv2.transpose(frame)
        new_frame = cv2.flip(new_frame, flipCode = 0)

    elif theta_deg == 270:

        # Rotate CCW 270 (CW 90)
        new_frame = cv2.transpose(frame)
        new_frame = cv2.flip(new_frame, flipCode = 1)

    elif theta_deg == 180:

        # Rotate by 180
        new_frame = cv2.flip(frame, flipCode = 0)
        new_frame = cv2.flip(new_frame, flipCode = 1)

    else: # Arbitrary rotation

        new_frame = rotate(frame, theta_deg, resize=True)

        # Scale and recast to uint8
        new_frame = np.uint8(new_frame * 255.0)

    return new_frame
Exemplo n.º 17
0
 def rotateImage(img, clockwise=True):
     #timg = np.zeros(img.shape[1],img.shape[0]) # transposed image
     if clockwise:
         # rotate counter-clockwise
         timg = cv2.transpose(img)
         cv2.flip(timg,flipCode=0)
         return timg
     else:
         # rotate clockwise
         timg = cv2.transpose(img)
         cv2.flip(timg,flipCode=1)
         return timg
Exemplo n.º 18
0
def hologram(infile, outfile, screen_below_pyramid=False):
    '''Transform infile video to a hologram video with no audio track and save to outfile'''
    capture = cv2.cv.CaptureFromFile(infile)
    # nbFrames = int(cv2.cv.GetCaptureProperty(capture, cv2.cv.CV_CAP_PROP_FRAME_COUNT))
    width = int(cv2.cv.GetCaptureProperty(capture, cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
    height = int(cv2.cv.GetCaptureProperty(capture, cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
    fps = cv2.cv.GetCaptureProperty(capture, cv2.cv.CV_CAP_PROP_FPS)
    # duration = (nbFrames / float(fps))

    length = request.form['length']
    d = request.form['d']
    padding = request.form['padding']
    assert 0 < int(length) <= 5000, 'Length is not in (0, 5000].'
    assert 0 < int(d) < int(length) / 2, 'd is not in (0, length/2).'
    assert 0 <= 2 * int(padding) < min(2 * int(d), int(length) / 2 - int(d)), 'Padding is too large.'
    length, d, padding = map(int, [length, d, padding])
    if length % 2:
        length += 1 # Keep length even for convenience
    cap = cv2.VideoCapture(infile)
    bgd = np.zeros((length, length, 3), np.uint8) # Create a black background
    new_wid = 2 * d - 2 * padding
    new_hgt = int(float(new_wid) / width * height)
    if new_hgt + d + 2 * padding > length / 2:
        new_hgt = length / 2 - d - 2 * padding
        new_wid = int(float(new_hgt) / height * width)
    if new_wid % 2:
        new_wid -= 1
    if new_hgt % 2:
        new_hgt -= 1

    fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v')
    out = cv2.VideoWriter(outfile, fourcc, fps, (length,length))

    while(1):
        ret, frame = cap.read()
        if not ret:
            break
        resized_frame = cv2.resize(frame, (new_wid, new_hgt))
        if screen_below_pyramid:
            resized_frame = cv2.flip(resized_frame, 0)
        bgd[length/2 + d + padding:length/2 + d + new_hgt + padding, length/2 - new_wid/2:length/2 + new_wid/2] =\
            resized_frame
        bgd[length/2 - d - padding - new_hgt:length/2 - d - padding, length/2 - new_wid/2:length/2 + new_wid/2] =\
            cv2.flip(resized_frame, -1)
        bgd[length/2 - new_wid/2:length/2 + new_wid/2, length/2 + d + padding:length/2 + d + new_hgt + padding] =\
            cv2.flip(cv2.transpose(resized_frame), 0)
        bgd[length/2 - new_wid/2:length/2 + new_wid/2, length/2 - d - padding - new_hgt:length/2 - d - padding] =\
            cv2.flip(cv2.transpose(resized_frame), 1)
        out.write(bgd)

    cap.release()
    out.release()
Exemplo n.º 19
0
    def crops(self, normalised, rotation=None):
        """Generator function that yields cropped images
        Rotation should be None, an int or an iterable. If not None, crops will
        be rotated by that many clockwise degrees.
        """
        import cv2
        import numpy as np

        if not rotation:
            rotation = repeat(0)
        elif isinstance(rotation, int):
            rotation = repeat(rotation)

        h, w = self.array.shape[:2]
        for box, rotate in zip(self.from_normalised(normalised), rotation):
            x0, y0, x1, y1 = box.coordinates
            x_in_bounds = [0 <= x0 <= w, 0 <= x1 <= w]
            y_in_bounds = [0 <= y0 <= h, 0 <= y1 <= h]
            if all(chain(x_in_bounds, y_in_bounds)):
                # View
                crop = self.array[y0:y1, x0:x1]
            else:
                # Box is out of bounds -create a new array, all zeroes (black)
                crop_w, crop_h = x1-x0, y1-y0
                crop = np.zeros((crop_h, crop_w, self.array.shape[2]),
                                dtype=self.array.dtype)
                if any(x_in_bounds) and any(y_in_bounds):
                    # Partial overlap
                    overlapping = self.array[max(y0, 0):min(y1, h),
                                             max(x0, 0):min(x1, w)]
                    dest_y, dest_x = max(0, 0-y0), max(0, 0-x0)
                    crop[dest_y:(dest_y + overlapping.shape[0]),
                         dest_x:(dest_x + overlapping.shape[1])] = overlapping

            if 0 != rotate % 90:
                msg = 'Rotation is not a multiple of 90: [{0}]'
                raise ValueError(msg.format(rotate))
            elif rotate:
                n_rotations = (rotate % 360) / 90
                # n_rotations will be 0, 1, 2 or 3 = the number of 90 degree
                # clockwise rotations
                if 1 == n_rotations:
                    # Rotate 90 clockwise
                    crop = cv2.flip(cv2.transpose(crop), 1)
                elif 2 == n_rotations:
                    # Rotate 180 clockwise
                    crop = cv2.flip(crop, -1)
                elif 3 == n_rotations:
                    # Rotate 90 counter-clockwise
                    crop = cv2.flip(cv2.transpose(crop), 0)

            yield crop
    def grab(self, index=0):
        """ Actually read a new frame at given index.

        Setting color flag to False will convert frames to greyscale.
        """
        if index != self.index:
            self.index = index
            rv, frame = self.capture.read()
            if rv:
                if self.color:
                    self.current_frame = cv2.transpose(cv2.cvtColor(frame, code=cv2.COLOR_BGR2RGB))
                else:
                    self.current_frame = cv2.transpose(cv2.cvtColor(frame, code=cv2.COLOR_BGR2GRAY))
                return self.current_frame
Exemplo n.º 21
0
def rotate_imgs(raw_image_right, raw_image_left):
	'''
		Rotates left and right images to proper orientation. Note this is done with transpose and flip
		as opposed to cv2.warpAffine to improve speed.
	''' 

	# Rotate left image
	image_left = cv2.transpose(raw_image_left)
	image_left = cv2.flip(image_left, 0)

	# Rotate right image
	image_right = cv2.transpose(raw_image_right)
	image_right = cv2.flip(image_right, 1)

	return image_right, image_left
def main():

    ip_cam_url = ''
    capture_rate = default_capture_rate
    argv_len = len(sys.argv)

    if argv_len > 1:
        ip_cam_url = sys.argv[1]
        
        if argv_len > 2 and sys.argv[2].isdigit():
            capture_rate = int(sys.argv[2])
    else:
        print("usage: video_cap_ipcam.py <ip-cam-url> [capture-rate]")
        return

    print("Capturing from '{}' at a rate of 1 every {} frames...".format(ip_cam_url, capture_rate))
    stream = urllib.urlopen(ip_cam_url)
    
    bytes = ''
    pool = Pool(processes=3)

    frame_count = 0
    while True:
        # Capture frame-by-frame
        frame_jpg = ''

        bytes += stream.read(16384*2)
        b = bytes.rfind('\xff\xd9')
        a = bytes.rfind('\xff\xd8', 0, b-1)


        if a != -1 and b != -1:
            #print 'Found JPEG markers. Start {}, End {}'.format(a,b)
            
            frame_jpg_bytes = bytes[a:b+2]
            bytes = bytes[b+2:]

            if frame_count % capture_rate == 0:
                
                #You can perform any image pre-processing here using OpenCV2.
                #Rotating image 90 degrees to the left:
                nparr = np.fromstring(frame_jpg_bytes, dtype=np.uint8)
                
                #Simple and efficient rotation: 90 degrees left = flip + transpose
                img_cv2_mat = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
                rotated_img = cv2.transpose(cv2.flip(img_cv2_mat, 0))
                
                #Computationally-intensive rotation
                # (h,w) = img_cv2_mat.shape[:2]
                # center = (w/2, h/2)

                # rot_mat = cv2.getRotationMatrix2D(center, -90, 1.0)
                # rotated = cv2.warpAffine(img_cv2_mat, rot_mat, (w, h))
                
                retval, new_frame_jpg_bytes = cv2.imencode(".jpg", rotated_img)

                #Send to Kinesis
                result = pool.apply_async(send_jpg, (bytearray(new_frame_jpg_bytes), frame_count, True, False, False,))

            frame_count += 1
Exemplo n.º 23
0
  def __init__(self, image, low, high):
    self.image_name = image
    self.low = low
    self.high = high
    self.filter = None
    self.output = None
    self.filter_type = None
    self.padRows = None
    self.padCols = None

    original_input = cv2.imread(image,0)
    rows, cols = original_input.shape
    if(rows < cols):
      original_input = cv2.transpose(original_input)
      self.transposed = True
    else:
      self.transposed = False
      
    rows, cols = original_input.shape
    optimalRows = cv2.getOptimalDFTSize(rows)
    optimalCols = cv2.getOptimalDFTSize(cols)
    self.padRows = optimalRows - rows
    self.padCols = optimalCols - cols
    self.input = np.zeros((optimalRows,optimalCols))
    self.input[:rows,:cols] = original_input
    
    self.dftshift = get_dft_shift(self.input)

    plt.subplots_adjust(left=0, bottom=0.05, right=1, top=1, wspace=0, hspace=0)

    plt.subplot(131)
    plt.axis('off')
    plt.title('original image')
    plt.imshow(self.input, cmap = 'gray',interpolation='nearest')

    
    self.axslow = plt.axes([0.05, 0.01, 0.5, 0.02])
    self.slow = Slider(self.axslow, 'low', 0.01, 1, valinit=self.low)
    self.slow.on_changed(self.updateLow)

    self.axhigh = plt.axes([0.05, 0.03, 0.5, 0.02])
    self.shigh = Slider(self.axhigh, 'high', 0.01, 1, valinit=self.high)
    self.shigh.on_changed(self.updateHigh)

    self.slow.slidermax=self.shigh
    self.shigh.slidermin=self.slow


    self.axfilter = plt.axes([0.62, 0.01, 0.15, 0.04])
    self.rfilter = RadioButtons(self.axfilter, ('Gaussian Filter', 'Ideal Filter'))
    self.rfilter.on_clicked(self.updateFilterType)
    self.filter_type = self.rfilter.value_selected
    
    self.axprocess = plt.axes([0.8, 0.01, 0.08, 0.04])
    self.bprocess = Button(self.axprocess, 'Process')
    self.bprocess.on_clicked(self.process)

    self.axsave = plt.axes([0.88, 0.01, 0.08, 0.04])
    self.bsave = Button(self.axsave, 'Save')
    self.bsave.on_clicked(self.save)
Exemplo n.º 24
0
 def save(self, unused):
   filter_file_title = os.path.splitext(self.image_name)[0] + '_' + self.filter_type + '_' + str(self.low)+'_'+str(self.high)+'_filter'
   processed_file_title = os.path.splitext(self.image_name)[0] + '_' + self.filter_type + '_' + str(self.low)+'_'+str(self.high)
   
   if(self.padRows != 0 or self.padCols != 0):
     rows, cols = self.output.shape
     processed = self.output[:rows-self.padRows, :cols-self.padCols]
   else:
     processed = self.output
   
   if(self.transposed):
     save_filter(cv2.transpose(self.filter), filter_file_title)
     save_image(cv2.transpose(processed), processed_file_title)
   else:
     save_filter(self.filter, filter_file_title)
     save_image(processed, processed_file_title)
Exemplo n.º 25
0
def rotateYlabel(label):
    """
    Function to rotate y axis label
    """
    label = cv2.transpose(label)
    label = cv2.flip(label, 1)
    return label
Exemplo n.º 26
0
def makePortrait(img):
    height = len(img)
    width = len(img[0])
    if width > height:
        return cv2.transpose(img)
    else:
        return img
Exemplo n.º 27
0
	def captureImage(self, mirror=False, flush=False, flushValue=1):
		""" If mirror is set to True, the image will be displayed as a mirror,
		otherwise it will be displayed as the camera sees it """
		if self.isConnected:
			self.reading = True
			if flush:
				for i in xrange(0, flushValue):
					self.capture.read() #grab()

			ret, image = self.capture.read()
			self.reading = False
			if ret:
				if self.useDistortion and \
				   self.cameraMatrix is not None and \
				   self.distortionVector is not None and \
				   self.distCameraMatrix is not None:
					mapx, mapy = cv2.initUndistortRectifyMap(self.cameraMatrix, self.distortionVector,
															 R=None, newCameraMatrix=self.distCameraMatrix,
															 size=(self.width, self.height), m1type=5)
					image = cv2.remap(image, mapx, mapy, cv2.INTER_LINEAR)
				image = cv2.transpose(image)
				if not mirror:
					image = cv2.flip(image, 1)
				self._success()
				return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
			else:
				self._fail()
				return None
		else:
			self._fail()
			return None
Exemplo n.º 28
0
    def set_cam_position(self):
        while True:
            ret, frame = self.capture.read()
            if ret:
                # Rotate and flip
                if self.cf["inverse"]:
                    rot = cv2.flip(cv2.transpose(frame), 1)
                else:
                    rot = frame
                # Display raw
                rot = self.rawWin.add_lines(rot, self.cf)
                self.rawWin.display(rot, self.cf)

                # Display grayscale image
                gray = cv2.cvtColor(rot, cv2.COLOR_BGR2GRAY)
                self.grayWin.display(gray, self.cf)

                # Display trackbar
                sw_old = self.barWin.win_set["laser"]
                self.barWin.display(gray, self.cf)
                sw_new = self.barWin.win_set["laser"]

                # Update laser
                laser_L_or_R(self.arduino, sw_old, sw_new)

                # wait for esc key to exit
                key = np.int16(cv2.waitKey(33))
                if key == 27:
                    break
            else:
                print("Webcam is busy")
        cv2.destroyAllWindows()
Exemplo n.º 29
0
 def _grabImage(self):
     w = self.display.widget
     rval, img = self.vc.read()
     if rval:
         # COLOR
         if self.pGrayscale.value():
             img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
         else:
             img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
         img = cv2.transpose(img)
         if self.pFloat.value():
             img = toFloatArray(img)
         i = w.image
         b = self.pBuffer.value()
         if b:
             # BUFFER LAST N IMAGES
             if i is None or len(i) < b:
                 self.display.addLayer(data=img)
             else:
                 # TODO: implement as ring buffer using np.roll()
                 img = np.insert(i, 0, img, axis=0)
                 img = img[:self.pBuffer.value()]
                 w.setImage(img, autoRange=False, autoLevels=False)
         else:
             w.setImage(img, autoRange=False, autoLevels=False)
Exemplo n.º 30
0
def resize_rotate_and_frame():
    print "Rotate and Frame Images"
    srcpp = "../data/dataset"
    dstpp = "../data/framed"
    imnames = imt.get_imlist(srcpp, ext = ".png")
    s=320
    i=0
    for imname in imnames:
        filename = imname.split('\\')[-1]
        ## === OPERATORS ===
        if 'dept' in imname:
            im = cv2.imread(imname, -1)
        else:
            im = cv2.imread(imname)

        b = imt.shrinkIm(im, s)
        
        #rotate the image:
        if 'right' in imname:
            b = np.fliplr(cv2.transpose(b))            
        elif 'head' in imname:
            b = cv2.flip(b, -1)
        #frame the image
        a = imt.frameImage(b)

        dst = ("/").join([dstpp,filename])
        print i, dst, b.shape
        i+=1
        cv2.imshow('rotated', a)            
        cv2.waitKey(5)
Exemplo n.º 31
0
                   0, 0, 0, 
                   1, 5, 1] )
kernel2 = array( [ -1, 0, 1, 
                   -5, 0, 5,
                   -1, 0, 1] )

gray_image = cv2.cvtColor(opencv_image, cv2.COLOR_BGR2GRAY)
filtered_image = cv2.filter2D(gray_image, -1, kernel1)#returns a 2d array
rows,cols = gray_image.shape
#print(gray_image.shape)

#print("==== Horizontal Histogram ====")
horz_hist = calculateHistogram(filtered_image, rows, cols)
horz_hist = removeModeAndLess(horz_hist)

gray_rot_90_image = cv2.transpose(gray_image)
gray_rot_90_image = cv2.flip(gray_rot_90_image, 1)
filtered_rot_90_image = cv2.filter2D(gray_rot_90_image, -1, kernel1)
rows,cols = gray_rot_90_image.shape

vert_hist = calculateHistogram(filtered_rot_90_image, rows, cols)
#vert_hist = removeModeAndLess(vert_hist)


horz_lines = findLines( horz_hist, 50 ) 
vert_lines = findLines( vert_hist, 50 ) 
print(horz_lines)
print(vert_lines)

'''
=====================================================================================================================
Exemplo n.º 32
0
def main(args):
    hp.check_directory_existence(args.out_path, exit_=False, create_=True)
    img_filenames = hp.get_all_files_in_dir_path(args.img_path,
                                                 extensions=hp.IMG_EXTENSIONS)
    img_filenames = sorted(img_filenames)

    for img_filename in img_filenames:
        img = hp.imread(img_filename)
        print(" # {} image file processing...".format(img_filename))
        pts1 = mg.define_quadrilateral(img)
        if pts1 == -1:
            sys.exit(1)
        elif pts1 == 0:
            continue
        pts1 = np.float32(pts1)
        h1_len = np.sqrt((pts1[0][0] - pts1[1][0])**2 +
                         (pts1[0][1] - pts1[1][1])**2)
        h2_len = np.sqrt((pts1[2][0] - pts1[3][0])**2 +
                         (pts1[2][1] - pts1[3][1])**2)
        v1_len = np.sqrt((pts1[0][1] - pts1[3][1])**2 +
                         (pts1[0][1] - pts1[3][1])**2)
        v2_len = np.sqrt((pts1[1][1] - pts1[2][1])**2 +
                         (pts1[1][1] - pts1[2][1])**2)
        h_len = int((h1_len + h2_len) / 2)
        v_len = int((v1_len + v2_len) / 2)
        pts2 = np.float32([[0, 0], [h_len, 0], [0, v_len], [h_len, v_len]])
        mtx = cv2.getPerspectiveTransform(pts1, pts2)
        warp_img = cv2.warpPerspective(img, mtx, (h_len, v_len))
        out_img = np.zeros((v_len + args.boundary_margin * 2,
                            h_len + args.boundary_margin * 2, 3),
                           dtype=np.uint8)
        out_img[args.boundary_margin:args.boundary_margin + v_len,
                args.boundary_margin:args.boundary_margin + h_len] = warp_img
        guide_on_ = False
        while True:
            if not guide_on_:
                img_zoom, _ = hp.imresize_full(out_img)
                disp_img = cv2.putText(
                    img_zoom, "Press \'r\', \'f\', \'m\', \'y\', or \'s\'",
                    (50, 60), CV2_FONT, 1, hp.RED, 4)
                cv2.imshow('warping', cv2.cvtColor(disp_img,
                                                   cv2.COLOR_RGB2BGR))
            in_key = cv2.waitKey(1) & 0xFF
            if in_key == ord('y'):
                # ans = input(" % Enter the image filename : ")
                break
            elif in_key == ord('m'):
                out_img = cv2.flip(out_img, 1)
                guide_on_ = False
            elif in_key == ord('f'):
                out_img = cv2.flip(out_img, 0)
                guide_on_ = False
            elif in_key == ord('r'):
                out_img = cv2.transpose(out_img)
                out_img = cv2.flip(out_img, 0)
                guide_on_ = False
            elif in_key == ord('s'):
                continue
            else:
                pass
        cv2.destroyAllWindows()
        for i in range(10):
            cv2.waitKey(1)
        while True:
            core_fname = os.path.splitext(os.path.basename(img_filename))[0]
            out_img_fname = os.path.join(args.out_path,
                                         core_fname + "--crop.jpg")
            out_json_fname = os.path.join(args.out_path,
                                          core_fname + "--crop.json")
            out_info_dict = {
                'image_filename': img_filename,
                'vertices': pts1.astype(np.int16).tolist(),
            }
            print(" # Default filename is {}".format(out_img_fname))
            ans = input(
                " % Enter the image filename (Enter to use the default filename) : "
            )
            ans = out_img_fname if ans == '' else ans
            if ans.split('.')[-1] in hp.IMG_EXTENSIONS:
                if os.path.isfile(out_img_fname):
                    print(" @ Warning: the same filename exists, {}.".format(
                        out_img_fname))
                else:
                    hp.imwrite(out_img, out_img_fname)
                    with open(out_json_fname, 'w') as f:
                        json.dump(out_info_dict, f)
                    break
            else:
                print(" @ Error: Image file extension required, {}".format(
                    ans.split('.')[-1]))
Exemplo n.º 33
0
import cv2
import numpy as np

image = cv2.imread('images/input.jpg')
height, width = image.shape[:2]

# Divide by two to rototate the image around its centre
rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), 90, .5)

rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))

cv2.imshow('Rotated Image', rotated_image)
cv2.waitKey()
cv2.destroyAllWindows()

#Other Option to Rotate But 90° at a time

img = cv2.imread('images/input.jpg')

rotated_image = cv2.transpose(img)

cv2.imshow('Rotated Image - Method 2', rotated_image)
cv2.waitKey()
cv2.destroyAllWindows()

# Now to a horizontal flip.
flipped = cv2.flip(image, 1)
cv2.imshow('Horizontal Flip', flipped) 
cv2.waitKey()
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 28 14:01:55 2018

@author: 박진우
"""

import cv2
vidcap = cv2.VideoCapture('./hoi_01.mp4')
success, image = vidcap.read()
count = 0
while success:
    # cv2.imwrite("frame%04d.png" % count, image)     # save frame as JPEG file

    # Image Rotate

    image = cv2.transpose(image)
    image = cv2.flip(image, 1)
    cv2.imwrite('./inputs/' + str(count) + ".jpg",
                image)  # save frame as JPEG file
    success, image = vidcap.read()
    print('Read a new frame: ', success)
    count += 1
Exemplo n.º 35
0
 def frm_pretreatment(self, ret, frame, crop, *preParam):
     dst = self.pretreatment(frame, *preParam)
     cropped = dst[crop[0][0]:crop[0][1], crop[1][0]:crop[1][1]]
     transposed = cv2.flip(cv2.transpose(cropped), 0)
     return transposed
Exemplo n.º 36
0
##Rotation
#  [

#    [ cos   - sin   ],

#      [  sin0     cos0 ]

#   ]

#cv2.getRotationMatrix( rotation_centreX , Rotation_centre_y , angel  , scale  )

image = cv2.imread('image.jpg')
height, width = image.shape[:2]

R = cv2.getRotationMatrix2D((width / 2, height / 2), 90, 1)

RotatedImage = cv2.warpAffine(image, R, (width, height))

cv2.imshow("Rotated Image", RotatedImage)

cv2.waitKey()
cv2.destroyAllWindows()

## Another Method   (short cut)

image = cv2.imread('image.jpg')
RotatedImage = cv2.transpose(image)
cv2.imshow("Transposed Image", RotatedImage)
cv2.waitKey()
cv2.destroyAllWindows()
Exemplo n.º 37
0
def unrotate(original, debug=False):
    mask = white_paper_mask(original, debug)
    #
    # scale down so that we can use consistent number of iterations for morphological ops
    #
    scale = 512 / original.shape[1]
    thresh = cv2.resize(mask, (0, 0), fx=scale, fy=scale)
    scaled = cv2.resize(original, (0, 0), fx=scale, fy=scale)

    num_iter = 1
    closed = cv2.dilate(cv2.erode(thresh, None, iterations=num_iter),
                        None,
                        iterations=num_iter)
    if debug:
        cv2.imshow(__file__, closed)
        cv2.waitKey()

    contours, _ = cv2.findContours(closed, cv.CV_RETR_EXTERNAL,
                                   cv.CV_CHAIN_APPROX_SIMPLE)

    if debug:
        cv2.drawContours(scaled, contours, -1, (255, 0, 0))
        cv2.imshow(__file__, scaled)
        cv2.waitKey()

    #
    # Pick the quad with the largest area
    #
    quads = []
    for contour in contours:
        #
        # TODO: check if the contour is rectangular enough
        #
        area = cv2.contourArea(contour)

        #
        # TODO: How to decide this threshold?
        # TODO: order the vertices in the decided polygon in some predictable way
        #
        poly = cv2.approxPolyDP(contour, 50, True)
        if len(poly) == 4:
            quads.append((area, poly))

    assert quads

    #
    # Pick the largest quad, by area
    #
    quad = sorted(quads, reverse=True)[0][1]
    a_x = quad[0][0][0]
    a_y = quad[0][0][1]
    b_x = quad[1][0][0]
    b_y = quad[1][0][1]
    d_x = quad[2][0][0]
    d_y = quad[2][0][1]
    e_x = quad[3][0][0]
    e_y = quad[3][0][1]
    width = math.sqrt((a_x - b_x)**2 + (a_y - b_y)**2)
    height = math.sqrt((b_x - d_x)**2 + (b_y - d_y)**2)

    if debug:
        cv2.circle(scaled, (int(a_x), int(a_y)), 5, (255, 0, 0), -1)  # blue
        cv2.putText(scaled, "A", (int(a_x), int(a_y)),
                    cv.CV_FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0))
        cv2.circle(scaled, (int(b_x), int(b_y)), 5, (0, 255, 0), -1)  # green
        cv2.putText(scaled, "B", (int(b_x), int(b_y)),
                    cv.CV_FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0))
        cv2.circle(scaled, (int(d_x), int(d_y)), 5, (0, 0, 255), -1)  # red
        cv2.putText(scaled, "D", (int(d_x), int(d_y)),
                    cv.CV_FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255))
        cv2.circle(scaled, (int(e_x), int(e_y)), 5, (255, 255, 0), -1)  # cyan
        cv2.putText(scaled, "E", (int(e_x), int(e_y)),
                    cv.CV_FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0))
        cv2.imshow(__file__, scaled)
        cv2.waitKey()

    #
    # Scale up to the original image and calculate a perspective transform
    #
    A_x, A_y, B_x, B_y, D_x, D_y, E_x, E_y = map(
        lambda x: x / scale, [a_x, a_y, b_x, b_y, d_x, d_y, e_x, e_y])
    Width, Height = width / scale, height / scale

    src = np.array([(A_x, A_y), (B_x, B_y), (D_x, D_y), (E_x, E_y)],
                   dtype="float32")
    dst = np.array([(Width, 0), (0, 0), (0, Height), (Width, Height)],
                   dtype="float32")

    perspective = cv2.getPerspectiveTransform(src, dst)

    warped = cv2.warpPerspective(original, perspective, (0, 0))
    warped = warped[:Height, :Width]
    if width > height:
        warped = cv2.flip(cv2.transpose(warped), 1)

    return warped
Exemplo n.º 38
0
def callback(msg_1, msg_2):
    global i
    global j
    global outlist
    j = j + 1
    if j == 1:
        i = i + 1
        # resize and crop image for msg_1(left side)
        cv2_msg_img = bridge.imgmsg_to_cv2(msg_1)
        cv_imgc = bridge.cv2_to_imgmsg(cv2_msg_img, 'rgb8')
        pil_img = encode(cv_imgc)
        fg_img = PILImage.new('RGBA', pil_img.size, (0, 0, 0, 255))
        draw = ImageDraw.Draw(fg_img)
        draw.ellipse(XYc, fill=(0, 0, 0, 0))  # 中心を抜く
        pil_img.paste(fg_img, (0, 0), fg_img.split()[3])
        img_msg = decode(pil_img)
        cv2_imgd = bridge.imgmsg_to_cv2(img_msg, 'rgb8')

        cv_cutimg = cv2_imgd[yc - xyoffset:yc + xyoffset,
                             xc - xyoffset:xc + xyoffset]
        cv_cutimg = cv2.transpose(cv_cutimg)
        cv_cutimg = cv2.flip(cv_cutimg, 1)
        #
        cv_resize1 = cv2.resize(cv_cutimg, (rsizex, rsizey))
        cv_resizex = cv_resize1.transpose(2, 0, 1)
        in_imgcc1 = np.array([cv_resizex], dtype=np.float32)
        in_img1 = (in_imgcc1 - 128) / 128
        #
        # resize and crop image for msg_2(right side)
        cv2_msg_img = bridge.imgmsg_to_cv2(msg_2)
        cv_imgc = bridge.cv2_to_imgmsg(cv2_msg_img, 'rgb8')
        pil_img = encode(cv_imgc)
        fg_img = PILImage.new('RGBA', pil_img.size, (0, 0, 0, 255))
        draw = ImageDraw.Draw(fg_img)
        draw.ellipse(XYc, fill=(0, 0, 0, 0))  # 中心を抜く
        pil_img.paste(fg_img, (0, 0), fg_img.split()[3])
        img_msg = decode(pil_img)
        cv2_imgd = bridge.imgmsg_to_cv2(img_msg, 'rgb8')

        cv_cutimg = cv2_imgd[yc - xyoffset:yc + xyoffset,
                             xc - xyoffset:xc + xyoffset]
        cv_cutimg = cv2.transpose(cv_cutimg)
        cv_cutimg = cv2.flip(cv_cutimg, 1)
        #
        cv_resize2 = cv2.resize(cv_cutimg, (rsizex, rsizey))
        cv_resizex = cv_resize2.transpose(2, 0, 1)
        in_imgcc2 = np.array([cv_resizex], dtype=np.float32)
        in_img2 = (in_imgcc2 - 128) / 128

        #concatenated image by left(in_img1) and right(in_img2)
        in_img = np.concatenate((in_img1, in_img2), axis=1)
        in_imgg = cuda.to_gpu(in_img)  # to gpu
        img_real = Variable(in_imgg)
        #
        with chainer.using_config('train', False), chainer.no_backprop_mode():
            img_gen = gen(invg(img_real))
            dis_real = dis(img_real)
            dis_gen = dis(img_gen)
            output = fl(img_real - img_gen, dis_real - dis_gen,
                        dis_real)  #traversable probablity

        out_gonogoc = cuda.to_cpu(output.data)  # to cpu

        gonet_out.publish(out_gonogoc)  #publish
        j = 0
Exemplo n.º 39
0
		cv.imshow("Blue",cv.merge([B,zeros,zeros]))
		t_img = cv.merge([B,zeros,zeros])
	elif choice=='9':
		#Green mask
		B,G,R=cv.split(img)
		zeros = np.zeros((height,width),dtype="uint8")
		cv.imshow("Blue",cv.merge([zeros,G,zeros]))
		t_img = cv.merge([zeros,G,zeros])
	elif choice=='10':
		#Red mask
		B,G,R=cv.split(img)
		zeros = np.zeros((height,width),dtype="uint8")
		cv.imshow("Blue",cv.merge([zeros,zeros,R]))
		t_img = cv.merge([zeros,zeros,R])
	elif choice=='11':
		t_img = cv.transpose(img)
		cv.imshow("Transformed Image",t_img)
	elif choice=='12':
		face_cascade=cv.CascadeClassifier("haarcascade_frontalface_alt.xml")
		gray_img=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
		faces=face_cascade.detectMultiScale(gray_img,scaleFactor=1.06,minNeighbors=6)
		for x,y,w,h in faces:
			img1=cv.rectangle(img,(x,y),(x+w,y+h),(0,0,255),3)
			cv2.imshow("Gray",img1)
	elif choice=='13':
		cv.destroyAllWindows()
		new_image = np.zeros(img.shape, img.dtype)
		#g(x) = alpha*f(x) + beta 
		alpha = float(input('Enter alpha value [1.0-3.0]:\n'))
		beta = 	int(input('Enter beta value [0-100]:\n'))
		print('This may take a while....')
Exemplo n.º 40
0
import cv2
import numpy as np

img = cv2.imread('..\\MasterOpenCV\\images\\IMG_7539.jpg')
image = cv2.resize(img, (960, 540))

height, width = image.shape[:2]

rotation_matrix = cv2.getRotationMatrix2D((width / 2, height / 2), 90, .5)

rotation_img = cv2.warpAffine(image, rotation_matrix, (width, height))
cv2.imshow("roatation image", rotation_img)
cv2.waitKey()
cv2.destroyAllWindows()

#alternate way

rotation_image = cv2.transpose(image)
cv2.imshow("alt rotation image", rotation_image)
cv2.waitKey()
cv2.destroyAllWindows()
Exemplo n.º 41
0
def preprocess(files):
    for path in files:
        filename, file_extension = os.path.splitext(path)
        print('file:', filename, file_extension)

        img = cv2.imread(path)
        height, width, channels = img.shape

        img_t = np.zeros((width, height, channels), img.dtype)
        cv2.transpose(img, img_t)
        cv2.flip(img_t, 1, img_t)

        img_gray = cv2.cvtColor(img_t, cv2.COLOR_BGR2GRAY)

        min_sum = sys.maxint
        opt_angle = 0
        angle = -1.5
        while angle < 1.6:
            rot_m = cv2.getRotationMatrix2D((width / 2, height / 2), angle, 1)
            img_gray_r = cv2.warpAffine(img_gray,
                                        rot_m, (height, width),
                                        borderMode=cv2.BORDER_CONSTANT,
                                        borderValue=white_color)
            sum = img_gray_r.sum(axis=1).min()
            if sum < min_sum:
                min_sum = sum
                opt_angle = angle
            angle += 0.1
        print('opt_angle:', opt_angle)

        rot_m = cv2.getRotationMatrix2D((width / 2, height / 2), opt_angle, 1)
        img_r = cv2.warpAffine(img_t,
                               rot_m, (height, width),
                               borderMode=cv2.BORDER_CONSTANT,
                               borderValue=white_color)

        img_gray = cv2.cvtColor(img_r, cv2.COLOR_BGR2GRAY)
        # score_x = img_gray[0:width, 0:600].sum(axis=0).argmin() + score_x_shift
        x_sums = img_gray.sum(axis=0)
        sum_threshold = x_sums.mean() - 3 * x_sums.std()
        score_x = score_x_shift + min_score_x
        for x_sum in x_sums[min_score_x:]:
            if x_sum < sum_threshold:
                break
            score_x += 1

        print('score_x:', score_x)

        if filename.split('/')[-1] == 'page-000':
            label_width = label_first_width
        else:
            label_width = label_other_width
        label_left_x = max(0, score_x - label_width)

        cv2.imwrite('%s.png' % filename, img_r)
        img_instr = cv2.copyMakeBorder(img_r[0:width, label_left_x:score_x],
                                       0,
                                       0,
                                       label_left_x,
                                       300,
                                       borderType=cv2.BORDER_CONSTANT,
                                       value=white_color)
        cv2.imwrite('%s-instr.png' % filename, img_instr)
Exemplo n.º 42
0
def RotateClockWise90(img):
    trans_img = cv2.transpose( img )
    new_img = cv2.flip(trans_img, 1)
    return new_img
Exemplo n.º 43
0
from dvs_msgs.msg import Event
from dvs_msgs.msg import EventArray
import cv2
import numpy as np
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import rosbag
import os
import time

rospy.init_node('dvsmsg')
t = rospy.Time.from_sec(1468939993.11)
bag = rosbag.Bag('/home/ubuntu/Documents/VINS/data/shapes_6dof_new2.bag', 'w')

num = 1
bridge = CvBridge()
for i in range(900):
    path = '/home/ubuntu/Downloads/image900/%d.bmp' % (i + 1)
    frame = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    img90 = cv2.transpose(frame)
    # cv2.imshow('s',frame)
    # cv2.waitKey(100)
    msg = bridge.cv2_to_imgmsg(img90, encoding="mono8")

    msgtime = t + rospy.Duration.from_sec(0.03 * (i + 1))
    msg.header.stamp = msgtime
    msg.header.seq = i
    msg.header.frame_id = 'world'
    print msgtime.to_sec()
    bag.write("/dvs/image_new", msg, msg.header.stamp)
Exemplo n.º 44
0
    d2[i] = aux[0]

coluna = linha
for i in linha:
    for j in coluna:
        if i == j:
            D[i][j] = d2[i]

T = D
#print 'T'
#print T
######################### Fazendo o hotf ###########3##############

T1 = numpy.linalg.inv(T)
print 1
Xt = cv2.transpose(matX)
print 2
ele = numpy.dot(Xt, T1)
print 3
paren = numpy.dot(ele, matX)
print 4
paren = numpy.linalg.inv(paren)
print 5
paren = numpy.dot(matX, paren)
print 6
hotf = numpy.dot(T1, paren)
print 7
hotf = numpy.dot(hotf, matC)

arq = open('hotf.txt', 'w')
linha = range(0, imgj, 1)
Exemplo n.º 45
0
def outerRectangle(image):

    height, width, channels = image.shape
    if width > height:
        image = cv2.transpose(image)
        image = cv2.flip(image, 1)

    # resize image so it can be processed
    image = cv2.resize(image, (1600, 1200))

    # creating copy of original image
    orig = image.copy()
    # convert to grayscale and blur to smooth
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    #blurred = cv2.medianBlur(gray, 5)

    edged = cv2.Canny(blurred, 0, 50)

    orig_edged = edged.copy()

    # find the contours in the edged image, keeping only the
    # largest ones, and initialize the screen contour
    (_, contours, _) = cv2.findContours(edged, cv2.RETR_LIST,
                                        cv2.CHAIN_APPROX_NONE)
    contours = sorted(contours, key=cv2.contourArea, reverse=True)

    # get approximate contour
    for c in contours:
        p = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * p, True)

        if len(approx) == 4:
            target = approx
            break

    # mapping target points to 800x800 quadrilateral
    approx = rectify(target)
    (tl, tr, br, bl) = approx

    # compute the width of the new image, which will be the
    # maximum distance between bottom-right and bottom-left
    # x-coordiates or the top-right and top-left x-coordinates
    widthA = np.sqrt(((br[0] - bl[0])**2) + ((br[1] - bl[1])**2))
    widthB = np.sqrt(((tr[0] - tl[0])**2) + ((tr[1] - tl[1])**2))
    maxWidth = max(int(widthA), int(widthB))

    # compute the height of the new image, which will be the
    # maximum distance between the top-right and bottom-right
    # y-coordinates or the top-left and bottom-left y-coordinates
    heightA = np.sqrt(((tr[0] - br[0])**2) + ((tr[1] - br[1])**2))
    heightB = np.sqrt(((tl[0] - bl[0])**2) + ((tl[1] - bl[1])**2))
    maxHeight = max(int(heightA), int(heightB))

    # now that we have the dimensions of the new image, construct
    # the set of destination points to obtain a "birds eye view",
    # (i.e. top-down view) of the image, again specifying points
    # in the top-left, top-right, bottom-right, and bottom-left
    # order
    dst = np.array([[0, 0], [maxWidth - 1, 0], [maxWidth - 1, maxHeight - 1],
                    [0, maxHeight - 1]],
                   dtype="float32")

    pts2 = np.float32([[0, 0], [800, 0], [800, 800], [0, 800]])

    M = cv2.getPerspectiveTransform(approx, pts2)

    dst = cv2.warpPerspective(orig, M, (800, 800))

    mask = np.ones(orig.shape, np.uint8)
    mask = cv2.bitwise_not(mask)
    x_offset = y_offset = 50
    mask[y_offset:y_offset + dst.shape[0],
         x_offset:x_offset + dst.shape[1]] = dst

    return mask
Exemplo n.º 46
0
import numpy as np
import cv2

input_image = cv2.imread("./images/Trump.jpg")

width, height = input_image.shape[:2]

cv2.imshow("Trump", input_image)

cv2.waitKey()

cv2.destroyAllWindows()

rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), 90, 1)

rotated_image = cv2.warpAffine(input_image, rotation_matrix, (width, height))

cv2.imshow("Rotated Trump", rotated_image)

cv2.waitKey()

cv2.destroyAllWindows()

transposed_rotation = cv2.transpose(input_image)

cv2.imshow("Transposed Rotation ",transposed_rotation)

cv2.waitKey()

cv2.destroyAllWindows()
Exemplo n.º 47
0
    print("Usage: python3 video_feed.py phone_ip_addr [record]")

record_bool = sys.argv[2] if len(sys.argv) > 2 else False

FPS = cap.get(5)  #ipcamera app locks this at 25
resolution = (int(cap.get(4)), int(cap.get(3)))  #vert to horiz res
print("FPS", FPS)
print("resolution", resolution)

if record_bool:
    fourcc = cv2.VideoWriter_fourcc(*"MJPG")
    out = cv2.VideoWriter("./assets/output.avi", fourcc, FPS, resolution)

while cap.isOpened():  #Joshua's doc says while True but I think that's a typo
    ret, frame = cap.read()
    frame = cv2.flip(cv2.transpose(frame), 0)
    if ret:
        cv2.imshow("Feed", frame)
        if record_bool:
            out.write(frame)
        key = cv2.waitKey(1) & 0xFF
        if key == ord(" "):  #save frame on spacebar
            img_file = "./assets/" + str(time.time()) + ".jpg"
            cv2.imwrite(img_file, frame)
            print(img_file, "saved")
        elif key == 27:  #esc
            break
    else:
        break

cap.release()
Exemplo n.º 48
0
 def update(self, frame):
     self.frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
     self.frame = cv2.transpose(self.frame)
Exemplo n.º 49
0
    def default_loop(self, usage):
        # 프레임 읽어들여서 왼쪽만 HSV 색공간으로 변환하기
        left_frame = self.frm_pretreatment(*self.video_left.read(),
                                           *LaneCam.xreadParam_L)
        right_frame = self.frm_pretreatment(*self.video_right.read(),
                                            *LaneCam.xreadParam_R)
        left_hsv = cv2.cvtColor(left_frame, cv2.COLOR_BGR2HSV)

        # HSV, RGB 필터링으로 영상을 이진화 함
        filtered_L = cv2.inRange(left_hsv, self.lower_yellow,
                                 self.upper_yellow)
        filtered_R = cv2.inRange(right_frame, self.lower_white,
                                 self.upper_white)

        # 좌, 우 영상을 붙임. 모니터링을 위한 부분.
        both = np.vstack((right_frame, left_frame))
        both_final = cv2.flip(cv2.transpose(both), 1)
        self.lane_cam_raw_frame.write(both_final)

        # ---------------------------------- 여기부터 왼쪽 차선 박스 쌓기 영역 ----------------------------------
        if self.left_previous_points is None:
            row_sum = np.sum(filtered_L[0:300, 200:300], axis=1)
            start_point = np.argmax(row_sum)

            # 차선의 실마리를 찾을 때, 길이가 7650 / 255 = 30픽셀 이상 될때만 차선으로 인정하고, 그렇지 않을 경우
            # 차선이 없는 것으로 간주함
            if (row_sum[start_point] > 7650):
                self.left_current_points = np.array([0] * 10)
                self.left_current_points[0] = start_point

                for i in range(1, 10):
                    reference = self.left_current_points[i -
                                                         1] - self.BOX_WIDTH

                    x1, x2 = 300 - 30 * i, 330 - 30 * i
                    y1 = self.left_current_points[i - 1] - self.BOX_WIDTH
                    y2 = self.left_current_points[i - 1] + self.BOX_WIDTH

                    small_box = filtered_L[y1:y2, x1:x2]
                    center_of_mass = self.findCenterofMass(small_box)

                    # 박스가 비어 있는 경우 -1을 저장
                    if center_of_mass == -1:
                        self.left_current_points[i] = -1
                    else:
                        location = reference + center_of_mass
                        # 질량중심 결과가 전체 영상을 벗어나지 않았을 때만 저장하고
                        if 0 <= location < 300:
                            self.left_current_points[i] = location
                        # 벗어나면 -1을 저장함
                        else:
                            self.left_current_points[i] = -1

            else:
                self.left_current_points = None

        else:
            for i in range(0, 10):

                if self.left_current_points[i] != -1:
                    reference = self.left_previous_points[i] - self.BOX_WIDTH

                    x1, x2 = 270 - 30 * i, 300 - 30 * i
                    y1 = self.left_previous_points[i] - self.BOX_WIDTH
                    y2 = self.left_previous_points[i] + self.BOX_WIDTH

                    small_box = filtered_L[y1:y2, x1:x2]
                    center_of_mass = self.findCenterofMass(small_box)

                    if center_of_mass == -1:
                        self.left_current_points[i] = -1
                    else:
                        location = reference + center_of_mass

                        if 0 <= location < 300:
                            self.left_current_points[i] = location
                        else:
                            self.left_current_points[i] = -1

                else:
                    if i == 0:
                        reference = self.left_previous_points[
                            1] - self.BOX_WIDTH
                        x1, x2 = 270, 300
                        y1 = self.left_previous_points[1] - self.BOX_WIDTH
                        y2 = self.left_previous_points[1] + self.BOX_WIDTH

                        small_box = filtered_L[y1:y2, x1:x2]
                        center_of_mass = self.findCenterofMass(small_box)

                        if center_of_mass == -1:
                            self.left_current_points[0] = -1
                        else:
                            location = reference + center_of_mass

                            if 0 <= location < 300:
                                self.left_current_points[0] = location
                            else:
                                self.left_current_points[0] = -1

                    else:
                        reference = self.left_previous_points[
                            i - 1] - self.BOX_WIDTH

                        x1, x2 = 270 - 30 * i, 300 - 30 * i
                        y1 = self.left_previous_points[i - 1] - self.BOX_WIDTH
                        y2 = self.left_previous_points[i - 1] + self.BOX_WIDTH

                        small_box = filtered_L[y1:y2, x1:x2]
                        center_of_mass = self.findCenterofMass(small_box)

                        if center_of_mass == -1:
                            self.left_current_points[i] = -1
                        else:
                            location = reference + center_of_mass

                            if 0 <= location < 300:
                                self.left_current_points[i] = location
                            else:
                                self.left_current_points[i] = -1

        if np.count_nonzero(
                self.left_current_points == -1) >= 5 and usage == 0:
            self.left_current_points = None
        if np.count_nonzero(
                self.left_current_points == -1) >= 9 and usage == 1:
            self.left_current_points = None
        self.left_previous_points = self.left_current_points
        # ---------------------------------- 여기까지 왼쪽 차선 박스 쌓기 영역 ----------------------------------

        # ---------------------------------- 여기부터 오른쪽 차선 박스 쌓기 영역 ----------------------------------
        if self.right_previous_points is None:
            row_sum = np.sum(filtered_R[0:300, 200:300], axis=1)
            start_point = np.argmax(row_sum)

            # 차선의 실마리를 찾을 때, 길이가 7650 / 255 = 30픽셀 이상 될때만 차선으로 인정하고, 그렇지 않을 경우
            # 차선이 없는 것으로 간주함
            if (row_sum[start_point] > 7650):
                self.right_current_points = np.array([0] * 10)
                self.right_current_points[0] = start_point

                for i in range(1, 10):
                    reference = self.right_current_points[i -
                                                          1] - self.BOX_WIDTH

                    x1, x2 = 300 - 30 * i, 330 - 30 * i
                    y1 = self.right_current_points[i - 1] - self.BOX_WIDTH
                    y2 = self.right_current_points[i - 1] + self.BOX_WIDTH

                    small_box = filtered_R[y1:y2, x1:x2]
                    center_of_mass = self.findCenterofMass(small_box)

                    # 박스가 비어 있는 경우 -1을 저장
                    if center_of_mass == -1:
                        self.right_current_points[i] = -1
                    else:
                        location = reference + center_of_mass
                        # 질량중심 결과가 전체 영상을 벗어나지 않았을 때만 저장하고
                        if 0 <= location < 300:
                            self.right_current_points[i] = location
                        # 벗어나면 -1을 저장함
                        else:
                            self.right_current_points[i] = -1

            else:
                self.right_current_points = None

        else:
            for i in range(0, 10):

                if self.right_current_points[i] != -1:
                    reference = self.right_previous_points[i] - self.BOX_WIDTH

                    x1, x2 = 270 - 30 * i, 300 - 30 * i
                    y1 = self.right_previous_points[i] - self.BOX_WIDTH
                    y2 = self.right_previous_points[i] + self.BOX_WIDTH

                    small_box = filtered_R[y1:y2, x1:x2]
                    center_of_mass = self.findCenterofMass(small_box)

                    if center_of_mass == -1:
                        self.right_current_points[i] = -1
                    else:
                        location = reference + center_of_mass

                        if 0 <= location < 300:
                            self.right_current_points[i] = location
                        else:
                            self.right_current_points[i] = -1

                else:
                    if i == 0:
                        reference = self.right_previous_points[
                            1] - self.BOX_WIDTH
                        x1, x2 = 270, 300
                        y1 = self.right_previous_points[1] - self.BOX_WIDTH
                        y2 = self.right_previous_points[1] + self.BOX_WIDTH

                        small_box = filtered_L[y1:y2, x1:x2]
                        center_of_mass = self.findCenterofMass(small_box)

                        if center_of_mass == -1:
                            self.right_current_points[0] = -1
                        else:
                            location = reference + center_of_mass

                            if 0 <= location < 300:
                                self.right_current_points[0] = location
                            else:
                                self.right_current_points[0] = -1

                    else:
                        reference = self.right_previous_points[
                            i - 1] - self.BOX_WIDTH

                        x1, x2 = 270 - 30 * i, 300 - 30 * i
                        y1 = self.right_previous_points[i - 1] - self.BOX_WIDTH
                        y2 = self.right_previous_points[i - 1] + self.BOX_WIDTH

                        small_box = filtered_R[y1:y2, x1:x2]
                        center_of_mass = self.findCenterofMass(small_box)

                        if center_of_mass == -1:
                            self.right_current_points[i] = -1
                        else:
                            location = reference + center_of_mass

                            if 0 <= location < 300:
                                self.right_current_points[i] = location
                            else:
                                self.right_current_points[i] = -1

        if np.count_nonzero(
                self.right_current_points == -1) >= 5 and usage == 0:
            self.right_current_points = None
        if np.count_nonzero(
                self.right_current_points == -1) >= 9 and usage == 1:
            self.right_current_points = None
        self.right_previous_points = self.right_current_points

        # ---------------------------------- 여기까지 오른쪽 차선 박스 쌓기 영역 ----------------------------------
        if usage == 0:
            if self.left_current_points is not None:
                xs_valid = []
                ys_L_valid = []

                for i in range(0, 10):
                    temp = self.left_current_points[i]
                    if temp != -1:
                        xs_valid.append(-30 * i)
                        ys_L_valid.append(-1 * temp)
                        cv2.line(filtered_L,
                                 (300 - 30 * i, temp - self.BOX_WIDTH),
                                 (300 - 30 * i, temp + self.BOX_WIDTH), 150)

                self.left_coefficients = np.polyfit(xs_valid, ys_L_valid, 2)

                xs_plot = np.array([1 * i for i in range(-299, 1)])
                ys_plot_L = np.array([
                    self.left_coefficients[2] + self.left_coefficients[1] * v +
                    self.left_coefficients[0] * v**2 for v in xs_plot
                ])

                transformed_x = xs_plot + 299
                transformed_y_L = 0 - ys_plot_L

                for i in range(0, 300):
                    cv2.circle(
                        filtered_L,
                        (int(transformed_x[i]), int(transformed_y_L[i])), 2,
                        150, -1)

            else:
                self.left_coefficients = None

            if self.right_current_points is not None:
                xs_valid = []
                ys_R_valid = []

                for i in range(0, 10):
                    temp = self.right_current_points[i]
                    if temp != -1:
                        xs_valid.append(-30 * i)
                        ys_R_valid.append(300 - temp)
                        cv2.line(filtered_R,
                                 (300 - 30 * i, temp - self.BOX_WIDTH),
                                 (300 - 30 * i, temp + self.BOX_WIDTH), 150)

                self.right_coefficients = np.polyfit(xs_valid, ys_R_valid, 2)

                xs_plot = np.array([1 * i for i in range(-299, 1)])
                ys_plot_R = np.array([
                    self.right_coefficients[2] +
                    self.right_coefficients[1] * v +
                    self.right_coefficients[0] * v**2 for v in xs_plot
                ])

                transformed_x = xs_plot + 299
                transformed_y_R = 299 - ys_plot_R

                for i in range(0, 300):
                    cv2.circle(
                        filtered_R,
                        (int(transformed_x[i]), int(transformed_y_R[i])), 2,
                        150, -1)

        else:
            self.right_coefficients = None

        filtered_both = np.vstack((filtered_R, filtered_L))
        final = cv2.flip(cv2.transpose(filtered_both), 1)

        self.lane_cam_frame.write(final)
Exemplo n.º 50
0
image_copy[50:150, 675:775] = image_rotated
plt.imshow(image_copy)
plt.show()

# %%
#' Tu sais, avec cv2, retourner verticalement, horizontalement et transposer l'image.

vertically_flipped_image = cv2.flip(image_copy, 0)
plt.imshow(vertically_flipped_image)
plt.show()

horizontally_flipped_image = cv2.flip(image_copy, 1)
plt.imshow(horizontally_flipped_image)
plt.show()

transposed_image = cv2.transpose(image_copy)
plt.imshow(transposed_image)
plt.show()

# %%
#' ### Appliquer des filtres
#'
#' Je sais appliquer un filtre gaussien (de taille 11) à l'image.

image_smoothed = cv2.GaussianBlur(image_copy, (11, 11), 1)
plt.imshow(image_smoothed)
plt.show()

# %%
#' #### Tu sais appliquer n'importe quel filtre à une image
#'
Exemplo n.º 51
0
    # 1D ->>>> 2D

    H2D = np.zeros((size_rows, size_cols, 2))

    rows = 0
    for j in xrange(0, size_cols):
        for i in xrange(0, size_rows):
            H2D[i, j, 0] = Hotf[rows, 0, 0]
            H2D[i, j, 1] = Hotf[rows, 0, 1]
            rows = rows + 1

    # Filtragem

    H2DTr = np.zeros((size_cols, size_rows, 2))
    H2DTr[:, :, 0] = cv2.transpose(H2D[:, :, 0])
    H2DTr[:, :, 1] = cv2.transpose(H2D[:, :, 1])

    ImgFilt = cv2.gemm(H2DTr, ImgDFT, 1, 0, 0)
    # ImgFilt = cv2.gemm(Hotf, ImgDFT, 1, 0, 0)

    cv2.dft(ImgFilt, ImgFiltIDFT, cv2.DFT_INVERSE)  # ?

    #print('{}, c: {} '.format(name, ImgFilt[0, 0, 0]))

    # Plot3D

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    x = np.arange(0, 80, 1)
Exemplo n.º 52
0
import numpy as np
from column_names import data

testData = pd.read_csv('../datasets/emnist-balanced-test.csv', names=data)
x_test = testData.loc[:, testData.columns != 'label']
y_test = testData['label']
x_test = x_test.to_numpy()
y_test = y_test.to_numpy()

temp = np.reshape(x_test[1], (28, 28))
temp = np.uint8(temp)
print(temp.shape)
print(temp)
cv2.imshow('image', temp)
cv2.waitKey(2000)
temp = cv2.transpose(temp)
cv2.imshow('image', temp)
cv2.waitKey(2000)
temp = cv2.transpose(temp)
cv2.imshow('image', temp)
cv2.waitKey(2000)

x_test = np.reshape(x_test, (-1, 28, 28, 1))

model = tf.keras.models.load_model("logs/modelsBalancedFinal/model1.hdf5")

for i in range(len(x_test)):
    y = model.predict_classes(np.reshape(x_test[i], (-1, 28, 28, 1)))
    print("Predicted: {}  Actual: {}".format(y, y_test[i]))
    if i == 1:
        break
Exemplo n.º 53
0
def TestFlappyBird(video_record = True):

    # Test AI for the Flappy Bird game
    # Initialize Flappy Bird game
    flappybird = game.GameState()
    
    # Load AI for the game
    num_actions = 2
    AI_player = DQN_AI(num_actions = num_actions, mode = 'test')
    AI_player.Load_Model()
	
    # AI starts to play the game
    # Initialize the first state of AI with the first observation from the game
    action = np.array([1,0])  # idle
    observation, reward, terminal = flappybird.frame_step(action)
    
    # Initialize video result log
    image_index = 0
    
    if video_record == True:
        if not os.path.exists(TEST_DIR + 'video/'):
            os.makedirs(TEST_DIR + 'video/')
        Clear_PNGs(folder_path = TEST_DIR + 'video/')
        cv2.imwrite(TEST_DIR + 'video/' + str(image_index) + '.png', cv2.cvtColor(cv2.transpose(observation), cv2.COLOR_BGR2RGB))
    
        if not os.path.exists(TEST_DIR + 'video_best/'):
            os.makedirs(TEST_DIR + 'video_best/')
        Clear_PNGs(folder_path = TEST_DIR + 'video_best/')
            
    observation = Preprocess(observation)
    AI_player.Current_State_Initialze(observation = observation)
    
    # Record score of each test
    test_round = 0
    score = 0
    score_highest = 0
    reward = 0
    terminal = False
    
    # Initialize test result log
    if not os.path.exists(TEST_DIR):
        os.makedirs(TEST_DIR)
    fhand = open(TEST_DIR + 'test_log.txt', 'w')
    fhand.write('TEST_ROUND\tSCORE')
    fhand.write('\n')
    fhand.close()
		
    # AI starts playing
    while True:
        # Keep playing until hitting 'ctrl + c'
        # print('time step: %d' % AI_player.time_step)
        
        if reward == 1:
            score += 1
        if terminal == True:
            # Save test result log
            print('test round: %d, score: %d' % (test_round, score))
            fhand = open(TEST_DIR + 'test_log.txt', 'a')
            fhand.write(str(test_round) + '\t' + str(score))
            fhand.write('\n')
            fhand.close()
            # Check if this round of test is best
            if video_record == True:
                if score > score_highest:
                    score_highest = score
                
                    # Clear video_best folder
                    Clear_PNGs(folder_path = TEST_DIR + 'video_best/')
                    # Copy all the images in the video folder to video_best folder
                    Copy_PNGs(source_path = TEST_DIR + 'video/', destination_path = TEST_DIR + 'video_best/')
                    # Clear video folder
                    Clear_PNGs(folder_path = TEST_DIR + 'video/')                       

            # Reset score
            score = 0
            # Reset image index
            image_index = 0
            # Increase test_round value
            test_round += 1

        action = AI_player.AI_Action()
        next_observation, reward, terminal = flappybird.frame_step(action)
        
        # Save animated images
        if video_record == True:
            cv2.imwrite(TEST_DIR + 'video/' + str(image_index) + '.png', cv2.cvtColor(cv2.transpose(next_observation), cv2.COLOR_BGR2RGB))
            image_index += 1
        
        next_observation = Preprocess(next_observation)
        AI_player.Current_State_Update(observation = next_observation)
Exemplo n.º 54
0
    def process(self, inframe, outframe):
        # Get the next camera image (may block until it is captured) and here convert it to OpenCV BGR. If you need a
        # grayscale image, just use getCvGRAY() instead of getCvBGR(). Also supported are getCvRGB() and getCvRGBA():
       # self.currentloopcount=self.currentloopcount+1
       # if self.currentloopcount==self.maxloopcount:
       #    self.currentimagecount=self.currentimagecount+1
       #    self.currentloopcount=0
       #    if self.currentimagecount>self.maximagecount:
       #       self.currentimagecount=self.minimagecount
       # self.currentimagecount=242
        
        imagefilename="practice"+str(self.currentimagecount)+".png"
        
#        inimg = cv2.imread(imagefilename+)
        inimg=cv2.imread("practice45719.png")      
        
#
#        inimg=inframe.getCvBGR()
        self.currentimagecount+=1
#        cv2.imwrite(imagefilename,inimg)
        inimg=cv2.transpose(inimg)
        inimg=cv2.flip(inimg, 1)
        

        # Start measuring image processing time (NOTE: does not account for input conversion time):
        self.timer.start()

        lowColor=np.array([53,20,211])
        highColor=np.array([86,255,255])
        imghls = cv2.cvtColor(inimg, cv2.COLOR_BGR2HLS)
      

        mask=cv2.inRange(imghls,lowColor,highColor)
        
        contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

 #now filter the contours
        
        biggestIndex=-1;
        secondIndex=-1;
        biggestSize=0;

        secondSize=0;
        index=0
        
        backtorgb=cv2.cvtColor(mask,cv2.COLOR_GRAY2BGR)
        if len(contours)>0:
           for c in contours:
               if cv2.contourArea(c)>biggestSize:
                  secondSize=biggestSize
                  secondIndex=biggestIndex
                  biggestSize=cv2.contourArea(c)
                  biggestIndex=index
          #        cv2.putText(backtorgb, str(cv2.contourArea(c)),(3, 233), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
               elif  cv2.contourArea(c)>secondSize:
                  secondSize=cv2.contourArea(c)
                  secondIndex=index
               index=index+1
               
           
        
        if biggestIndex>-1:
           cv2.drawContours(backtorgb, contours, biggestIndex, (0,255,0), 1)
           brect1=cv2.minAreaRect(contours[biggestIndex])
           brectPoints=cv2.boxPoints(brect1)
           brectPoints=np.int0(brectPoints)
           cv2.drawContours(backtorgb,[brectPoints],0,(0,0,255),1)
           boxText="("+str(brectPoints[0][0])+","+str(brectPoints[0][1])+")"+"("+str(brectPoints[1][0])+","+str(brectPoints[1][1])+")""("+str(brectPoints[2][0])+","+str(brectPoints[2][1])+")""("+str(brectPoints[3][0])+","+str(brectPoints[3][1])+")"
           #cv2.putText(backtorgb, boxText,(3, 233), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
           if self.written==False:
              self.datafile.write(boxText)
              self.datafile.write("\n")
        if secondIndex>-1:
           cv2.drawContours(backtorgb, contours, secondIndex, (0,255,0), 1)
           brect2=cv2.minAreaRect(contours[secondIndex])
           brectPoints2=cv2.boxPoints(brect2)

           brectPoints2=np.int0(brectPoints2)
           boxText="("+str(brectPoints2[0][0])+","+str(brectPoints2[0][1])+")"+"("+str(brectPoints2[1][0])+","+str(brectPoints2[1][1])+")""("+str(brectPoints2[2][0])+","+str(brectPoints2[2][1])+")""("+str(brectPoints2[3][0])+","+str(brectPoints2[3][1])+")"
           cv2.putText(backtorgb, boxText,(3, 233), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
           cv2.drawContours(backtorgb,[brectPoints2],0,(0,0,255),1)
           if self.written==False:
              self.datafile.write(boxText)
              self.datafile.write("\n")
            

           #if you are here, there must be two contours.  Find the appropriate
           #corners

           topmost=321 #there ought to be an easier way, but......
           secondmost=321
           toppointindex=-1
           secondpointindex=-1
           
           for index in range(0,4):
              if brectPoints[index][1]<topmost:
                secondpointindex=toppointindex
                toppointindex=index
                secondmost=topmost
                topmost=brectPoints[index][1]
                if self.written==False:
                    self.datafile.write("Topmost index is now "+str(index)+"\n")
              elif brectPoints[index][1]<secondmost:
                secondpointindex=index
                secondmost=brectPoints[index][1]
                if self.written==False:
                    self.datafile.write("Secondmost index is now "+str(index)+"\n")
                
           topcorner1=brectPoints[toppointindex]
           secondcorner1=brectPoints[secondpointindex]

           topmost=321 #there ought to be an easier way, but......
           secondmost=321
           toppointindex=-1
           secondpointindex=-1
           if self.written==False:
              self.datafile.write("Next set of points \n")
           for index in range(0,4):
              if self.written==False:
                 self.datafile.write(str(topmost)+" "+str(secondmost)+" "+str(brectPoints2[index][1])+"\n")
              if brectPoints2[index][1]<topmost:
                secondpointindex=toppointindex
                toppointindex=index
                secondmost=topmost
                topmost=brectPoints2[index][1]
                if self.written==False:
                    self.datafile.write("Topmost index is now "+str(index)+"\n")

              elif brectPoints2[index][1]<secondmost:
                secondpointindex=index
                secondmost=brectPoints2[index][1]
                if self.written==False:
                    self.datafile.write("Secondmost index is now "+str(index)+"\n")
                
           topcorner2=brectPoints2[toppointindex]
           secondcorner2=brectPoints2[secondpointindex]
           #Almost there - set up the arrays of object and image points
           #cv2.putText(backtorgb, str(topcorner2[0])+" "+str(topcorner2[1]),(3, 45), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
      
           objpoints=np.array(((-5.94,0.5,0),(-4,0,0),(4,0,0),(5.94,0.5,0)),dtype=np.float)
           x1=topcorner1[0]
           x2=topcorner2[0]
           if x1<x2:
               imagepoints=np.array([[topcorner1[0],topcorner1[1]],[secondcorner1[0],secondcorner1[1]],[secondcorner2[0],secondcorner2[1]],[topcorner2[0],topcorner2[1]]],dtype=np.float)
               
           else:
               imagepoints=np.array([[topcorner2[0],topcorner2[1]],[secondcorner2[0],secondcorner2[1]],[secondcorner1[0],secondcorner1[1]],[topcorner1[0],topcorner1[1]]],dtype=np.float)
         #  imagepoints=np.array(((50*(-5.94)+320,50*0.5+240),(50*(-4)+320,0+240),(50*4+320,0+240),(50*5.94+320,50*0.5+240)),dtype=np.float)
              

           #imagepoints=np.array(((186,220),(188,212),(187,171),(186,164)),dtype=np.float)
           cv2.putText(backtorgb, str(imagepoints[0][0])+" "+str(imagepoints[0][1]),(3, 65), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
           cv2.putText(backtorgb, str(imagepoints[1][0])+" "+str(imagepoints[1][1]),(3, 85), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
           cv2.putText(backtorgb, str(imagepoints[2][0])+" "+str(imagepoints[2][1]),(3, 105), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
           cv2.putText(backtorgb, str(imagepoints[3][0])+" "+str(imagepoints[3][1]),(3, 125), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
      
           cv2.putText(backtorgb, str(topcorner1[0])+" "+str(topcorner1[1]),(160,65),  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
           cv2.putText(backtorgb, str(secondcorner1[0])+" "+str(secondcorner1[1]),(160,85),  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
           cv2.putText(backtorgb, str(topcorner2[0])+" "+str(topcorner2[1]),(160,105),  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
           cv2.putText(backtorgb, str(secondcorner2[0])+" "+str(secondcorner2[1]),(160,125),  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
           #The moment of truth?
           errorestimate,rvec,tvec=cv2.solvePnP(objpoints,imagepoints,self.cameraMatrix,self.distcoeff)

           #cv2.putText(backtorgb, str(tvec[0])+" "+str(tvec[1])+" "+str(tvec[2]),(3, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
           cv2.putText(backtorgb, "%.2f" % tvec[0]+" "+"%.2f" % tvec[1]+" "+"%.2f" % tvec[2],(3, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
           
           cv2.putText(backtorgb, "%.2f" % (rvec[0]*180/3.14159)+" "+  "%.2f" % (rvec[1]*180/3.14159)+" "+"%.2f" % (rvec[2]*180/3.14159),(3, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
          
           #spit things out on the serial port, but first, do some testing.  Print stuff out.                   
                    

              
                   

        
 
 
 
 
       # cv2.putText(outimg, fps, (3, height - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255))
        
        # Convert our output image to video output format and send to host over USB:
        outframe.sendCv(backtorgb)
        self.written=True
Exemplo n.º 55
0
import numpy as np
import os
from helper import detector_helper as helper

image_dir = "../622data/test/businessRuleTask/"


def format_text(text):
    res = text.strip()
    res = res.replace("\n", " ")
    res = res.replace("  ", " ")
    return res


for name in os.listdir(image_dir)[25:30]:
    print(name)
    image_path = image_dir + name
    image = cv.imread(image_path)

    # text = pytesseract.image_to_string(Image.fromarray(image_mat))
    # text = format_text(text)
    #
    # print(text)
    # print("=" * 30)
    cv.imshow("img", helper.dilate_drawing(image))
    rotated_img = cv.transpose(image)
    rotated_img = cv.flip(rotated_img, 1)
    cv.imshow("rotated", helper.dilate_drawing(rotated_img))
    cv.waitKey(0)
    # np.rot
Exemplo n.º 56
0
def rotate_image_counter_clockwise(image):
    image = cv2.transpose(image)
    image = cv2.flip(image, 0)
    return image
Exemplo n.º 57
0
#참고한 코드 https://webnautes.tistory.com/577 , https://goo.gl/NNBStM
import cv2
from PIL import Image
cam = cv2.VideoCapture('video.mp4')
cntvid = 0
while (cam.isOpened()):
    ret, image = cam.read()
    image_rotate = cv2.transpose(image)
    image_rotate = cv2.flip(image_rotate, 1)
    fps = cam.get(cv2.CAP_PROP_FPS)
    if (int(cam.get(1)) % 5 == 0):
        cv2.imwrite("frames/frame%d.png" % cntvid, image_rotate)
        cntvid += 1
    if cntvid == 12: break
#작업 종료시키기
cam.release()
cv2.destroyAllWindows()

#이미지 변환
for i in range(cntvid):
    #print (i)
    path = 'frames/frame' + str(i) + '.png'
    save_path = 're_frames/re_' + str(i) + '.png'
    im_r = Image.open(path)
    size = (28, 28)
    im_r.thumbnail(size)
    im_r.save(save_path)
Exemplo n.º 58
0
 def rot90(img):
     timg = cv2.transpose(img)
     timg = cv2.flip(timg, 0)
     return timg
Exemplo n.º 59
0
import cv2
import numpy as np
from src.basic import imageInfo

image = cv2.imread(imageInfo.photographyImage)
height, width = image.shape[:2]

#divide by rwo to rate the image around its center
rotationMatrix = cv2.getRotationMatrix2D((width / 2, height / 2), 45, 0.5)
print(rotationMatrix)

rotatedImage = cv2.warpAffine(image, rotationMatrix, (width, height))

rotatedByTranspose = cv2.transpose(image)

cv2.imshow("Rotated image", rotatedImage)
cv2.imshow("Rotated image by transpose", rotatedByTranspose)
cv2.waitKey()
cv2.destroyAllWindows()
Exemplo n.º 60
0
    #cv2.imshow("img", paper)
    #cv2.waitKey(0)
    #cv2.destroyAllWindows()

#################################################################################################################################

#############################################  STEP 4 :Correct orientation of page ##############################################

    height = paper.shape[0]
    width = paper.shape[1]

    # Rotate the image such that the page is correctly oriented
    paper = rgb_paper.copy()

    for c in range(rotation_count):
        paper = cv2.transpose(paper)
        paper = cv2.flip(paper, flipCode=1)
        warped = cv2.transpose(warped)
        warped = cv2.flip(warped, flipCode=1)

    # display the result

    #cv2.imshow("rotated page", cv2.resize(paper,(620,620)))
    #cv2.waitKey(0)

#########################################################################################################################################

############################################### STEP 5 :Template matching for find our page #############################################

    paper_gray = cv2.cvtColor(paper, cv2.COLOR_BGR2GRAY)