def threshold_image(): """ runs the image processing in order to create a black and white thresholded image out of D.image into D.threshed_image. """ # get D so that we can change values in it global D # Use OpenCV to split the image up into channels, saving them in gray images cv.Split(D.image, D.blue, D.green, D.red, None) # This line creates a hue-saturation-value image cv.CvtColor(D.image, D.hsv, cv.CV_RGB2HSV) cv.Split(D.hsv, D.hue, D.sat, D.val, None) # Here is how OpenCV thresholds the images based on the slider values: cv.InRangeS(D.red, D.thresholds["low_red"], D.thresholds["high_red"], D.red_threshed) cv.InRangeS(D.blue, D.thresholds["low_blue"], D.thresholds["high_blue"], D.blue_threshed) cv.InRangeS(D.green, D.thresholds["low_green"], D.thresholds["high_green"], D.green_threshed) cv.InRangeS(D.hue, D.thresholds["low_hue"], D.thresholds["high_hue"], D.hue_threshed) cv.InRangeS(D.sat, D.thresholds["low_sat"], D.thresholds["high_sat"], D.sat_threshed) cv.InRangeS(D.val, D.thresholds["low_val"], D.thresholds["high_val"], D.val_threshed) # Multiply all the thresholded images into one "output" image, D.threshed_image cv.Mul(D.red_threshed, D.green_threshed, D.threshed_image) cv.Mul(D.threshed_image, D.blue_threshed, D.threshed_image) cv.Mul(D.threshed_image, D.hue_threshed, D.threshed_image) cv.Mul(D.threshed_image, D.sat_threshed, D.threshed_image) cv.Mul(D.threshed_image, D.val_threshed, D.threshed_image)
def create_image(self): #Find the size of the image #Create images for each channel blue = cv.CreateImage(self.size, 8, 1) red = cv.CreateImage(self.size, 8, 1) green = cv.CreateImage(self.size, 8, 1) hue = cv.CreateImage(self.size, 8, 1) sat = cv.CreateImage(self.size, 8, 1) val = cv.CreateImage(self.size, 8, 1) #Create an image to be returned and eventually displayed thresholds = cv.CreateImage(self.size, 8, 1) #Create images to save the thresholded images to red_threshed = cv.CreateImage(self.size, 8, 1) green_threshed = cv.CreateImage(self.size, 8, 1) blue_threshed = cv.CreateImage(self.size, 8, 1) hue_threshed = cv.CreateImage(self.size, 8, 1) sat_threshed = cv.CreateImage(self.size, 8, 1) val_threshed = cv.CreateImage(self.size, 8, 1) #Split the image up into channels, saving them in their respective image cv.Split(self.image, blue, green, red, None) cv.CvtColor(self.image, self.hsv, cv.CV_RGB2HSV) cv.Split(self.hsv, hue, sat, val, None) #Threshold the images based on the slider values cv.InRangeS(red, self.thresholds['low_red'],\ self.thresholds['high_red'], red_threshed) cv.InRangeS(green, self.thresholds['low_green'],\ self.thresholds['high_green'], green_threshed) cv.InRangeS(blue, self.thresholds['low_blue'],\ self.thresholds['high_blue'], blue_threshed) cv.InRangeS(hue, self.thresholds['low_hue'],\ self.thresholds['high_hue'], hue_threshed) cv.InRangeS(sat, self.thresholds['low_sat'],\ self.thresholds['high_sat'], sat_threshed) cv.InRangeS(val, self.thresholds['low_val'],\ self.thresholds['high_val'], val_threshed) #Recombine all of the thresholded images into one image cv.Mul(red_threshed, green_threshed, thresholds) cv.Mul(thresholds, blue_threshed, thresholds) cv.Mul(thresholds, hue_threshed, thresholds) cv.Mul(thresholds, sat_threshed, thresholds) cv.Mul(thresholds, val_threshed, thresholds) #Erode and Dilate shave off and add edge pixels respectively cv.Erode(thresholds, thresholds, iterations=1) cv.Dilate(thresholds, thresholds, iterations=1) return thresholds
def TransformImage(self, bgr, hsv, channels, size, method): assert(bgr.type == cv.CV_8UC3) assert(hsv.type == cv.CV_8UC3) chs_all = [ cv.CreateMat(hsv.rows, hsv.cols, cv.CV_8UC1) for i in range(0, 6) ] cv.Split(bgr, chs_all[CH_BLUE], chs_all[CH_GREEN], chs_all[CH_RED], None) cv.Split(hsv, chs_all[CH_HUE], chs_all[CH_SAT], chs_all[CH_VAL], None) chs = [ cv.GetImage(chs_all[ch]) for ch in channels ] dst = cv.CreateMat(hsv.rows - size[1] + 1, hsv.cols - size[0] + 1, cv.CV_32FC1) cv.CalcBackProjectPatch(chs, dst, size, self.hist_pos, method, 1.0) return dst
def hs_histogram(src, patch): # Convert to HSV hsv = cv.CreateImage(cv.GetSize(src), 8, 3) cv.CvtColor(src, hsv, cv.CV_BGR2HSV) hsv_patch= cv.CreateImage(cv.GetSize(patch), 8, 3) # Extract the H and S planes h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) h_plane_img = cv.CreateImage(cv.GetSize(src), 8, 1) h_plane_patch = cv.CreateMat(patch.rows, patch.cols, cv.CV_8UC1) s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) s_plane_img = cv.CreateImage(cv.GetSize(src), 8, 1) s_plane_patch = cv.CreateMat(patch.rows, patch.cols, cv.CV_8UC1) v_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) cv.Split(hsv, h_plane, s_plane, v_plane, None) cv.Split(hsv, h_plane_img, s_plane_img, None, None) cv.Split(hsv_patch, h_plane_patch, s_plane_patch, None, None) #cv.Split(src, h_plane, s_plane, v_plane, None) planes = [h_plane_patch, s_plane_patch]#, s_plane, v_plane] h_bins = 30 s_bins = 32 v_bins = 30 hist_size = [h_bins, s_bins] # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */ h_ranges = [0, 180] # saturation varies from 0 (black-gray-white) to # 255 (pure spectrum color) s_ranges = [0, 255] v_ranges = [0, 255] ranges = [h_ranges, s_ranges]#, s_ranges, v_ranges] scale = 10 hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1) cv.CalcHist([cv.GetImage(i) for i in planes], hist) (_, max_value, _, _) = cv.GetMinMaxHistValue(hist) hist_img = cv.CreateImage((h_bins*scale, s_bins*scale), 8, 3) back_proj_img = cv.CreateImage(cv.GetSize(src), 8, 1) cv.CalcBackProject([h_plane_img, s_plane_img], back_proj_img, hist) # for h in range(h_bins): # for s in range(s_bins): # bin_val = cv.QueryHistValue_2D(hist, h, s) # intensity = cv.Round(bin_val * 255 / max_value) # cv.Rectangle(hist_img, # (h*scale, s*scale), # ((h+1)*scale - 1, (s+1)*scale - 1), # cv.RGB(intensity, intensity, intensity), # cv.CV_FILLED) return back_proj_img, hist
def image_callback(data): global running if (running): image = bridge.imgmsg_to_cv(data, "bgr8") #normalize image cv.Split(image, rgb_r, rgb_g, rgb_b, None) red_mean = cv2.mean(np.asarray(rgb_r[:, :])) cv.Div(src2=cv.fromarray(np.ones((480, 640))), src1=rgb_r, dst=scaled_r, scale=128 / red_mean[0]) green_mean = cv2.mean(np.asarray(rgb_g[:, :])) cv.Div(src2=cv.fromarray(np.ones((480, 640))), src1=rgb_g, dst=scaled_g, scale=128 / green_mean[0]) blue_mean = cv2.mean(np.asarray(rgb_b[:, :])) cv.Div(src2=cv.fromarray(np.ones((480, 640))), src1=rgb_b, dst=scaled_b, scale=128 / blue_mean[0]) cv.Merge(scaled_r, scaled_g, scaled_b, None, cv_image) cv.CvtColor(cv_image, hsv, cv.CV_BGR2HSV) # --convert from BGR to HSV cv.CvtColor(cv_image, lab, cv.CV_BGR2Lab) cv.Split(hsv, hsv_h, hsv_s, hsv_v, None) cv.Split(cv_image, rgb_r, rgb_g, rgb_b, None) cv.Split(lab, lab_l, lab_a, lab_b, None) cv.Split(luv, luv_l, luv_u, luv_v, None) cv.Split(hls, hls_h, hls_l, hls_s, None) cv.Split(xyz, xyz_x, xyz_y, xyz_x, None) cv.Split(ycrcb, ycrcb_y, ycrcb_cr, ycrcb_cb, None) cv.Not(lab_a, a_not) cv.Sub(hsv_s, a_not, sa) cv.Sub(luv_u, hls_h, test) cv.Sub(hls_s, hls_h, sminh) threshold_red(sa) cv.ShowImage("red", red_dilated_image) red_contours, _ = cv2.findContours(image=np.asarray( red_dilated_image[:, :]), mode=cv.CV_RETR_EXTERNAL, method=cv.CV_CHAIN_APPROX_SIMPLE) print_lidar_projections(cv_image) circles = extract_circles(red_contours, [1, 0, 0]) for x, y, radius in circles: cv.Circle(cv_image, (x, y), radius, [0, 0, 1], 3) cv.SetMouseCallback("camera feed", mouse_callback, hsv_image) cv.ShowImage("camera feed", cv_image) cv.WaitKey(3)
def wximg_to_cv(wximg, flatten=False): """ Converts a WxImage to a OpenCV IplImage, with optional conversion to Grayscale (single channel). """ w, h = wximg.GetWidth(), wximg.GetHeight() #wximg = wximg.Copy() if flatten: wximg = wximg.ConvertToGreyscale() #data = wximg.GetData() #cv_im = cv.CreateImage((w, h), cv.IPL_DEPTH_8U, 3) #cv.SetData(cv_im, data) #cv_mat = cv.CreateMat(h,w, cv.CV_8UC3) #cv.SetData(cv_mat, data) #cv_mathead= cv.CreateMatHeader(h, w, cv.CV_8UC3) #cv.CreateData(cv_mathead) #cv.SetData(cv_mat, data) cv_mat = cv.CreateMat(h, w, cv.CV_8UC3) cv.SetData(cv_mat, wximg.GetData()) #cv.Copy(data, cv_mat) if flatten: #cv.SetImageCOI(cv_im, 1) #res = cv.CreateImage((w,h), cv.IPL_DEPTH_8U, 1) #cv.Copy(cv_im, res) #return res res = cv.CreateMat(h, w, cv.CV_8UC1) cv.Split(cv_mat, res, None, None, None) return res else: return cv_mat
def get_three_planes(src): channels = [None] * 4 size = cv.GetSize(src) for i in range(src.channels): channels[i] = cv.CreateImage(size, src.depth, 1) cv.Split(src, channels[0], channels[1], channels[2], None) return channels[:3]
def hsv_orange_red_threshold(input_image): blur_image = cv.CreateMat(input_image.rows, input_image.cols, cv.CV_8UC3) cv.Smooth(input_image, blur_image, cv.CV_BLUR, 10, 10) proc_image = cv.CreateMat(input_image.rows, input_image.cols, cv.CV_8UC3) cv.CvtColor(blur_image, proc_image, cv.CV_BGR2HSV) split_image = [ cv.CreateMat(input_image.rows, input_image.cols, cv.CV_8UC1), cv.CreateMat(input_image.rows, input_image.cols, cv.CV_8UC1), cv.CreateMat(input_image.rows, input_image.cols, cv.CV_8UC1) ] cv.Split(proc_image, split_image[0], split_image[1], split_image[2], None) thresh_0 = cv.CreateMat(input_image.rows, input_image.cols, cv.CV_8UC1) thresh_1 = cv.CreateMat(input_image.rows, input_image.cols, cv.CV_8UC1) thresh_2 = cv.CreateMat(input_image.rows, input_image.cols, cv.CV_8UC1) red_orange = cv.CreateMat(input_image.rows, input_image.cols, cv.CV_8UC1) cv.Threshold(split_image[1], thresh_0, 128, 255, cv.CV_THRESH_BINARY) # > 50% saturation cv.Threshold(split_image[0], thresh_1, 220, 255, cv.CV_THRESH_BINARY) # > Purple cv.Threshold(split_image[0], thresh_2, 10, 255, cv.CV_THRESH_BINARY_INV) # < Yellow-Orange cv.Add(thresh_1, thresh_2, red_orange) cv.And(red_orange, thresh_0, red_orange) return red_orange
def hs_histogram(src): # Convert to HSV hsv = cv.CreateImage(cv.GetSize(src), 8, 3) cv.CvtColor(src, hsv, cv.CV_BGR2HSV) # Extract the H and S plane h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) cv.Split(hsv, h_plane, s_plane, None, None) planes = [h_plane, s_plane] h_bins = 30 s_bins = 32 hist_size = [h_bins, s_bins] # hue varies from 0 to 180 h_ranges = [0, 180] # saturation varies from 0 to 255 s_ranges = [0, 255] ranges = [h_ranges, s_ranges] scale = 10 hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1) cv.CalcHist([cv.GetImage(i) for i in planes], hist) (_, max_value, _, _) = cv.GetMinMaxHistValue(hist) hist_img = cv.CreateImage((h_bins * scale, s_bins * scale), 8, 3) for h in range(h_bins): for s in range(s_bins): bin_val = cv.QueryHistValue_2D(hist, h, s) intensity = cv.Round(bin_val * 255 / max_value) cv.Rectangle(hist_img, (h * scale, s * scale), ((h + 1) * scale - 1, (s + 1) * scale - 1), cv.RGB(intensity, intensity, intensity), cv.CV_FILLED) return hist_img
def compute_histogram(self, img, bin_fact=cv.IPL_DEPTH_8U, min_range=0): """ Creates the histogram of the input image Returns a Tippy histogram, with one item by channel of input image. The number of bins is 2^bin_fact (256 by default) min_range is the value of the lowest bin max_range is automatically calculated using the input image depth. max_range = (2^img_depth) NOTE : Supports only 8 bits images for now. More should be added soon. """ # TESTS try: dims = cv.GetSize(img) except TypeError: raise TypeError("(%s) img : IplImage expected!" % (sys._getframe().f_code.co_name)) # img test #if not(img.depth == cv.IPL_DEPTH_8U): # raise TypeError("(%s) 8U image expected!" # % (sys._getframe().f_code.co_name)) if img.nChannels not in [1, 3]: raise ValueError("(%s) 1 or 3 channels image expected!" % (sys._getframe().f_code.co_name)) # bin_fact test if (bin_fact <= 0 or ((bin_fact % 2) != 0)): raise ValueError("(%s) Positive odd integer expected!" % (sys._getframe().f_code.co_name)) elif type(bin_fact) not in [int, long]: raise TypeError("(%s) Positive odd integer expected!" % (sys._getframe().f_code.co_name)) # min_range test if type(min_range) != int: raise TypeError("(%s) Positive odd integer expected!" % (sys._getframe().f_code.co_name)) self.channels = img.nChannels self.nb_bins = int(pow(2, bin_fact)) self.depth = int(img.depth) max_range = pow(2, bin_fact) + min_range - 1 self.ranges = [min_range, max_range] img_list = [] # preparing images depending on nChannels if self.channels == 1: img_list = [img] elif self.channels == 3: # TODO: change this into function img_1 = cv.CreateImage(dims, cv.IPL_DEPTH_8U, 1) img_2 = cv.CreateImage(dims, cv.IPL_DEPTH_8U, 1) img_3 = cv.CreateImage(dims, cv.IPL_DEPTH_8U, 1) cv.Split(img, img_1, img_2, img_3, None) img_list = [img_1, img_2, img_3] self.cvhist = self._compute_1ch_histogram(img_list)
def update_hue(self, frame): """ Convert frame to HSV and keep the hue """ hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) self.hue = cv.CreateImage(cv.GetSize(frame), 8, 1) cv.Split(hsv, self.hue, None, None, None)
def main(args): if args.output_directory: directory = args.output_directory else: directory = os.path.dirname(args.input_image) print "No output directory specified. Defaulting to %s" % directory if not os.path.exists(directory): os.makedirs(directory) if args.output_prefix: prefix = args.output_prefix extension = os.path.splitext(os.path.basename(args.input_image))[1] else: prefix, extension = os.path.splitext(os.path.basename( args.input_image)) print "No output prefix selected. Defaulting to %s" % prefix output_file = "%s/%s%s" % (directory, prefix, extension) image = cv.LoadImage(args.input_image) input_image = cv.LoadImage(args.input_image) mask = cv.CreateImage(cv.GetSize(input_image), 8, 1) image_hue = cv.CreateImage(cv.GetSize(input_image), 8, 1) image_hsv = cv.CreateImage(cv.GetSize(input_image), 8, 3) cv.CvtColor(input_image, image_hsv, cv.CV_BGR2HSV) cv.Split(image_hsv, image_hue, None, None, None) upper_thresh = cv.CreateImage(cv.GetSize(input_image), 8, 1) lower_thresh = cv.CreateImage(cv.GetSize(input_image), 8, 1) cv.Threshold(image_hue, upper_thresh, args.hue_max, 255, cv.CV_THRESH_BINARY) cv.Threshold(image_hue, lower_thresh, args.hue_min, 255, cv.CV_THRESH_BINARY_INV) cv.Or(upper_thresh, lower_thresh, mask) cv.SaveImage(output_file, mask)
def threshold(self, frame, record, op=cv.And, magic=False): """Threshold a frame using a record of min/max thresholds Output is a new image. """ colorspace, min, max = record tmp = cv.CloneImage(frame) if magic: cv.Smooth(tmp, tmp, cv.CV_GAUSSIAN, 11) # Work in the correct colorspace self.colorspaceConv[colorspace](tmp) num_chans = len(min) size = cv.GetSize(frame) chan = [ cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) for _ in range(num_chans) ] cv.Split(tmp, chan[0], chan[1], chan[2], None) minS = map(cv.Scalar, min) maxS = map(cv.Scalar, max) for i in range(num_chans): cv.InRangeS(chan[i], minS[i], maxS[i], chan[i]) out = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) op(chan[0], chan[1], out) op(out, chan[2], out) return out
def split3(cvimg): size = (cvimg.width, cvimg.height) c1 = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) c2 = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) c3 = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) cv.Split(cvimg, c1, c2, c3, None) return c1, c2, c3
def hist_eq(self, frame): cv.Split(frame, self.B, self.G, self.R, None) cv.EqualizeHist(self.R, self.R) cv.EqualizeHist(self.R, self.B) cv.EqualizeHist(self.G, self.G) cv.Merge(self.B, self.G, self.R, None, self.Ieq) return self.Ieq
def get_image(): global corners corners = [] im = cv.QueryFrame(camera) im_rgb = cv.GetMat(im) cv.CvtColor(im, im_rgb, cv.CV_BGR2RGB) #getting the rgbimage #worst image grayscaling of all time. #image = cv.CreateImage(cv.GetSize(im),cv.IPL_DEPTH_32F,3) #cv.ConvertScale(im,image) #new_image = cv.CreateImage(cv.GetSize(im),cv.IPL_DEPTH_32F,1) #cv.CvtColor(image,new_image,cv.CV_BGR2GRAY) #gray_image = cv.CreateImage(cv.GetSize(im),cv.IPL_DEPTH_8U,1) #cv.ConvertScale(new_image,gray_image) yuv = cv.CreateImage(cv.GetSize(im), 8, 3) gray_image = cv.CreateImage(cv.GetSize(im), 8, 1) cv.CvtColor(im, yuv, cv.CV_BGR2YCrCb) cv.Split(yuv, gray_image, None, None, None) eig_image = cv.CreateImage(cv.GetSize(gray_image), cv.IPL_DEPTH_32F, 1) temp_image = cv.CreateImage(cv.GetSize(gray_image), cv.IPL_DEPTH_32F, 1) for (x, y) in cv.GoodFeaturesToTrack(gray_image, eig_image, temp_image, 300, 0.01, 1.0, useHarris=True): corners.append([WIDTH - x, y]) return pygame.transform.flip( pygame.image.frombuffer(im_rgb.tostring(), cv.GetSize(im_rgb), "RGB"), True, False)
def processFrames(self): self.vidcap = cv2.VideoCapture(self.path) count = 0 success, image = self.vidcap.read() print success self.createWindows() while True: success, image = self.vidcap.read() if not success: return spare = cv.fromarray(image) size = (spare.width / 2, spare.height / 2) cv.Smooth(spare, spare, cv.CV_GAUSSIAN, BLUR_SIZE, BLUR_SIZE) out = cv.CreateImage(size, 8, 3) cv.PyrDown(spare, out) yuv = cv.CreateImage(size, 8, 3) gray = cv.CreateImage(size, 8, 1) canny = cv.CreateImage(size, 8, 1) sobel = cv.CreateImage(size, 8, 1) harris = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1) cv.CvtColor(out, yuv, cv.CV_BGR2YCrCb) cv.Split(yuv, gray, None, None, None) cv.Canny(gray, canny, 50, 200, 3) cv.CornerHarris(gray, harris, 3) cv.Sobel(gray, sobel, 1, 0, 3) cv.ConvertScale(canny, canny, -1, 255) cv.ConvertScale(sobel, sobel, -1, 255) for y in range(0, out.height): for x in range(0, out.width): harr = cv.Get2D(sobel, y, x) if harr[0] < 10e-06: cv.Circle(out, (x, y), 2, cv.RGB(155, 0, 25)) #cv2.imwrite("frame%d.jpg" % count, np.asarray(canny[:,:])) cv.ShowImage('canny', canny) #cv.ShowImage( 'harris' , harris ) cv.ShowImage('sobel', sobel) cv.ShowImage('output', out) if cv2.waitKey(1) == 27: break count += 1 return
def calcHistogram(self, image): cv.CvtColor(image, self.tmp, cv.CV_BGR2HSV) cv.Split(self.tmp, self.B, self.G, self.R, None) channels = self.B, self.G, self.R hist_props = [{} for channel in channels] #Normalisation constant for histogram size size_norm = 255.0 / self.hist_size for chnum, ch in enumerate(channels): #, colour, Ypos in zip(channels, values, positions): cv.CalcHist([ch], self.hist, 0, None) hist_arr = self.smoothHistogram() diffs = np.diff(hist_arr, 1) hist_props[chnum]['plateaus'] = [] mins = [(0, 0)] for i, v in enumerate(diffs): if v < mins[-1][1]: mins.append((i, v)) if hist_arr[i] > 1 and diffs[i - 1] * diffs[i] <= 0: hist_props[chnum]['plateaus'].append(i * size_norm) hist_props[chnum]['mins'] = [(i * size_norm, v) for i, v in mins] #print "MINS:",mins # Calculate beyond the 100th percentile due to numerical instability ptile = range(0, 103, 1) total = sum(hist_arr) running_sum = 0 for i, v in enumerate(hist_arr): running_sum += v while running_sum >= total * ptile[0] / 100.0: hist_props[chnum][ptile[0]] = i * size_norm del ptile[0] # Make sure all percentiles are actually in the data structure: prev = 10 for p in ptile: if p in hist_props[chnum]: prev = p else: hist_props[chnum][p] = hist_props[chnum][prev] assert 90 in hist_props[chnum], "percentile calculation incomplete" post_peaks = mins[-1][0] for i in range(mins[-1][0], len(diffs)): if diffs[i - 1] * diffs[i] <= 0: post_peaks = i #print hist_arr[i:] break #print "POST:",post_peaks*size_norm hist_props[chnum]['post_peaks'] = post_peaks * size_norm #self.drawHistogram(tmp, chnum, hist_arr, plateaus) return hist_props
def hough_extractor(self, side): # create space for a single channel blue image (the robot is red, so I don't want to use that) blue_image = cv.CreateImage((self.width/2, self.height), 8, 1)# half-width, for splitting down the middle #cv.CvtColor(self.cv_image, blue_image, cv.CV_BGR2GRAY) if side = "left":# the image is divided down the middle, so only look at appropriate side cv.Split(self.left_image, blue_image)
def channels_split(image): # -- split channels into individual channels channel = [] # -- all rows and cols will have 8 bit unisg value for x in range(0, 3): channel.append(cv.CreateMat(image.rows, image.cols, cv.CV_8UC1)) cv.Split(image, channel[0], channel[1], channel[2], None) return channel
def split3(iplimage): if iplimage.nChannels != 3: raise ValueError("The channel is not consistent.") b = cv.CreateImage((iplimage.width, iplimage.height), cv.IPL_DEPTH_8U, 1) g = cv.CreateImage((iplimage.width, iplimage.height), cv.IPL_DEPTH_8U, 1) r = cv.CreateImage((iplimage.width, iplimage.height), cv.IPL_DEPTH_8U, 1) cv.Split(iplimage, b, g, r, None) return b, g, r
def getBrightness(img): hue = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U,1) sat = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U,1) val = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U,1) test = cv.CloneImage(img) cv.CvtColor(img,test, cv.CV_BGR2HSV) cv.Split(img, hue, sat,val,None) return cv.Avg(val)[0]
def split(im): if im.nChannels == 3: imr = new_from(im, nChannels=1) img = new_from(im, nChannels=1) imb = new_from(im, nChannels=1) cv.Split(im, imr, img, imb, None) return (imr, img, imb) else: return (im,)
def cutLetters(image, thresh, log): mask = createMask(image, thresh) log.log(mask) h = cv.CreateImage(cv.GetSize(image), image.depth, 1) cv.CvtColor(image, image, cv.CV_BGR2HSV) cv.Split(image, h, None, None, None) log.log(h) cv.Set(h, cv.ScalarAll(255), mask) return h
def get_raw_frame(self): # Assumes that we are going to debayer the image later, so # returns a single channel image. im = cv.QueryFrame(self.capture) if im == None: raise NoFrameException('') cv.Split(im, self.gray_image, None, None, None) return self.gray_image
def run(self): hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1) backproject_mode = False while True: frame = cv.QueryFrame(self.capture) # Convert to HSV and keep the hue hsv = cv.CreateImage(cv.GetSize(frame), 8, 3) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) self.hue = cv.CreateImage(cv.GetSize(frame), 8, 1) print(self.hue) cv.Split(hsv, self.hue, None, None, None) # Compute back projection backproject = cv.CreateImage(cv.GetSize(frame), 8, 1) # Run the cam-shift cv.CalcArrBackProject([self.hue], backproject, hist) if self.track_window and is_rect_nonzero(self.track_window): crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1) (iters, (area, value, rect), track_box) = cv.CamShift(backproject, self.track_window, crit) self.track_window = rect # If mouse is pressed, highlight the current selected rectangle # and recompute the histogram if self.drag_start and is_rect_nonzero(self.selection): sub = cv.GetSubRect(frame, self.selection) save = cv.CloneMat(sub) cv.ConvertScale(frame, frame, 0.5) cv.Copy(save, sub) x, y, w, h = self.selection cv.Rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255)) sel = cv.GetSubRect(self.hue, self.selection) cv.CalcArrHist([sel], hist, 0) (_, max_val, _, _) = cv.GetMinMaxHistValue(hist) if max_val != 0: cv.ConvertScale(hist.bins, hist.bins, 255. / max_val) elif self.track_window and is_rect_nonzero(self.track_window): cv.EllipseBox(frame, track_box, cv.CV_RGB(255, 0, 0), 3, cv.CV_AA, 0) if not backproject_mode: #frame=cv.Flip(frame) cv.ShowImage("CamShiftDemo", frame) else: cv.ShowImage("CamShiftDemo", backproject) cv.ShowImage("Histogram", self.hue_histogram_as_image(hist)) c = cv.WaitKey(7) if c == 27: break elif c == ord("b"): backproject_mode = not backproject_mode
def detect_and_draw(img, cascade): # allocate temporary images gray = cv.CreateImage((img.width, img.height), 8, 1) small_img = cv.CreateImage((cv.Round( img.width / image_scale), cv.Round(img.height / image_scale)), 8, 1) # convert color input image to grayscale cv.CvtColor(img, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) window = cv.CreateImage((cv.Round(img.width), cv.Round(img.height)), 8, 3) if (cascade): t = cv.GetTickCount() faces = local_haar_detect(small_img, cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) t = cv.GetTickCount() - t print "detection time = %gms" % (t / (cv.GetTickFrequency() * 1000.)) channels = None if faces: for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (cv.Round( (x + w * .2) * image_scale), cv.Round(y * image_scale)) pt2 = (cv.Round( (x + w * .8) * image_scale), cv.Round( (y + h) * image_scale)) window = cv.CreateImage((cv.Round(w * .6) * image_scale, cv.Round(h) * image_scale), 8, 3) cv.Smooth(window, window, cv.CV_GAUSSIAN) channels = [ cv.CreateImage((cv.Round(w * .6) * image_scale, cv.Round(h) * image_scale), 8, 1), cv.CreateImage((cv.Round(w * .6) * image_scale, cv.Round(h) * image_scale), 8, 1), cv.CreateImage((cv.Round(w * .6) * image_scale, cv.Round(h) * image_scale), 8, 1) ] cv.GetRectSubPix(img, window, (cv.Round( (pt1[0] + pt2[0]) / 2.0), cv.Round( (pt1[1] + pt2[1]) / 2.0))) cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) cv.Split(window, channels[0], channels[1], channels[2], None) result.append([ cv.Avg(channels[0])[0], cv.Avg(channels[1])[0], cv.Avg(channels[2])[0] ]) cv.ShowImage("result", img)
def calc_hist(self): self.hist = cv.CreateHist([self.h_bins, self.s_bins], cv.CV_HIST_ARRAY, self.ranges, 1) hsv = cv.CreateImage(cv.GetSize(self.background_noise[0]), 8, 3) h_plane = cv.CreateMat(self.background_noise[0].height, self.background_noise[0].width, cv.CV_8UC1) s_plane = cv.CreateMat(self.background_noise[0].height, self.background_noise[0].width, cv.CV_8UC1) for i in xrange(len(self.background_noise)): cv.CvtColor(self.background_noise[i], hsv, cv.CV_BGR2HSV) cv.Split(hsv, h_plane, s_plane, None, None) planes = [h_plane, s_plane]#, s_plane, v_plane] cv.CalcHist([cv.GetImage(i) for i in planes], self.hist, True, self.mask)
def process_rgb(self, dev, data, timestamp): #global keep_running # get an opencv version of video_cv data frame = frame_convert.video_cv(data) frame_size = cv.GetSize(frame) # Convert to HSV and keep the hue hsv = cv.CreateImage(frame_size, 8, 3) cv.CvtColor(frame, hsv, cv.CV_BGR2HSV) self.hue = cv.CreateImage(frame_size, 8, 1) # split the image into different hues cv.Split(hsv, self.hue, None, None, None) # Compute back projection # Run the cam-shift backproject = cv.CreateImage(frame_size, 8, 1) cv.CalcArrBackProject([self.hue], backproject, self.hist) # if we have a tracking window... shift it # Track_window => (rectangle of approx hue) if self.track_window and is_rect_nonzero(self.track_window): # set criteria for backproject iter # compute back projections - shifting rectangle in # appropriate direction crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1) (iters, (area, value, rect), self.track_box) = cv.CamShift(backproject, self.track_window, crit) # set track_window to the newly selected rectangle self.track_window = rect # if a section is being selected - set the histogram if self.debug: sel = self.dbg_rgb.check_for_selection(self.track_window, self.track_box) # sets the histogram if there is a selection if sel: self.set_hist(frame, sel) self.dbg_rgb.update(frame) #if self.track_window: # self.dbg_rgb.add_box(self.track_box) self.dbg_rgb.render() # Bail out if ESC is pushed key = cv.WaitKey(3) char = chr(key & 255) # k is for KILL if char == 'k': self.keep_running = False else: self.curr_classifier().respond_to_key(char)
def sat_threshold(image, min_sat): image_hsv = cv.CloneImage(image) cv.CvtColor(image, image_hsv, cv.CV_RGB2HSV) image_sat = cv.CreateImage(cv.GetSize(image_hsv), 8, 1) cv.Split(image_hsv, None, image_sat, None, None) sat_thresh = cv.CloneImage(image_sat) cv.Threshold(image_sat, sat_thresh, min_sat, 255, cv.CV_THRESH_BINARY) image_out = cv.CloneImage(image) cv.Zero(image_out) cv.Copy(image, image_out, sat_thresh) return image_out