Python cv2.cv.CvtColor() Examples
The following are 10
code examples of cv2.cv.CvtColor().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
cv2.cv
, or try the search function
.
Example #1
Source File: motion-detection.py From rpi-opencv with GNU General Public License v3.0 | 6 votes |
def processImage(self, curframe): cv.Smooth(curframe, curframe) #Remove false positives if not self.absdiff_frame: #For the first time put values in difference, temp and moving_average self.absdiff_frame = cv.CloneImage(curframe) self.previous_frame = cv.CloneImage(curframe) cv.Convert(curframe, self.average_frame) #Should convert because after runningavg take 32F pictures else: cv.RunningAvg(curframe, self.average_frame, 0.05) #Compute the average cv.Convert(self.average_frame, self.previous_frame) #Convert back to 8U frame cv.AbsDiff(curframe, self.previous_frame, self.absdiff_frame) # moving_average - curframe cv.CvtColor(self.absdiff_frame, self.gray_frame, cv.CV_RGB2GRAY) #Convert to gray otherwise can't do threshold cv.Threshold(self.gray_frame, self.gray_frame, 50, 255, cv.CV_THRESH_BINARY) cv.Dilate(self.gray_frame, self.gray_frame, None, 15) #to get object blobs cv.Erode(self.gray_frame, self.gray_frame, None, 10)
Example #2
Source File: camshift.py From PyCV-time with MIT License | 6 votes |
def hue_histogram_as_image(self, hist): """ Returns a nice representation of a hue histogram """ histimg_hsv = cv.CreateImage( (320,200), 8, 3) mybins = cv.CloneMatND(hist.bins) cv.Log(mybins, mybins) (_, hi, _, _) = cv.MinMaxLoc(mybins) cv.ConvertScale(mybins, mybins, 255. / hi) w,h = cv.GetSize(histimg_hsv) hdims = cv.GetDims(mybins)[0] for x in range(w): xh = (180 * x) / (w - 1) # hue sweeps from 0-180 across the image val = int(mybins[int(hdims * x / w)] * h / 255) cv.Rectangle( histimg_hsv, (x, 0), (x, h-val), (xh,255,64), -1) cv.Rectangle( histimg_hsv, (x, h-val), (x, h), (xh,255,255), -1) histimg = cv.CreateImage( (320,200), 8, 3) cv.CvtColor(histimg_hsv, histimg, cv.CV_HSV2BGR) return histimg
Example #3
Source File: engine.py From opencv-engine with MIT License | 5 votes |
def image_data_as_rgb(self, update_image=True): # TODO: Handle other formats if self.image_channels == 4: mode = 'BGRA' elif self.image_channels == 3: mode = 'BGR' else: mode = 'BGR' rgb_copy = cv.CreateImage((self.image.width, self.image.height), 8, 3) cv.CvtColor(self.image, rgb_copy, cv.CV_GRAY2BGR) self.image = rgb_copy return mode, self.image.tostring()
Example #4
Source File: engine.py From opencv-engine with MIT License | 5 votes |
def convert_to_grayscale(self): if self.image_channels >= 3: # FIXME: OpenCV does not support grayscale with alpha channel? grayscaled = cv.CreateImage((self.image.width, self.image.height), self.image_depth, 1) cv.CvtColor(self.image, grayscaled, cv.CV_BGRA2GRAY) self.image = grayscaled
Example #5
Source File: engine.py From opencv-engine with MIT License | 5 votes |
def enable_alpha(self): if self.image_channels < 4: with_alpha = cv.CreateImage( (self.image.width, self.image.height), self.image_depth, 4 ) if self.image_channels == 3: cv.CvtColor(self.image, with_alpha, cv.CV_BGR2BGRA) else: cv.CvtColor(self.image, with_alpha, cv.CV_GRAY2BGRA) self.image = with_alpha
Example #6
Source File: tcg_ocr_scanner.py From tcg-ocr-scanner with GNU General Public License v3.0 | 5 votes |
def doGreyscaleProcessor(self, img): (x, y, w, h) = self.frame.rect bw = cv.CreateImage((w, h), 8, 1) cv.CvtColor(img, bw, cv.CV_RGB2GRAY) return bw
Example #7
Source File: Input.py From DanceCV with BSD 3-Clause "New" or "Revised" License | 5 votes |
def getCurrentFrameAsImage(self): im = numpy.array(self.currentFrame) im = cv.fromarray(im) cv.CvtColor(im, im, cv.CV_BGR2RGB) pgImg = pygame.image.frombuffer(im.tostring(), cv.GetSize(im), "RGB") return pgImg
Example #8
Source File: facedetect.py From PyCV-time with MIT License | 5 votes |
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) if(cascade): t = cv.GetTickCount() faces = cv.HaarDetectObjects(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.)) 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 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) cv.ShowImage("result", img)
Example #9
Source File: motempl.py From PyCV-time with MIT License | 4 votes |
def update_mhi(img, dst, diff_threshold): global last global mhi global storage global mask global orient global segmask timestamp = time.clock() / CLOCKS_PER_SEC # get current time in seconds size = cv.GetSize(img) # get current frame size idx1 = last if not mhi or cv.GetSize(mhi) != size: for i in range(N): buf[i] = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) cv.Zero(buf[i]) mhi = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1) cv.Zero(mhi) # clear MHI at the beginning orient = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1) segmask = cv.CreateImage(size,cv. IPL_DEPTH_32F, 1) mask = cv.CreateImage(size,cv. IPL_DEPTH_8U, 1) cv.CvtColor(img, buf[last], cv.CV_BGR2GRAY) # convert frame to grayscale idx2 = (last + 1) % N # index of (last - (N-1))th frame last = idx2 silh = buf[idx2] cv.AbsDiff(buf[idx1], buf[idx2], silh) # get difference between frames cv.Threshold(silh, silh, diff_threshold, 1, cv.CV_THRESH_BINARY) # and threshold it cv.UpdateMotionHistory(silh, mhi, timestamp, MHI_DURATION) # update MHI cv.CvtScale(mhi, mask, 255./MHI_DURATION, (MHI_DURATION - timestamp)*255./MHI_DURATION) cv.Zero(dst) cv.Merge(mask, None, None, None, dst) cv.CalcMotionGradient(mhi, mask, orient, MAX_TIME_DELTA, MIN_TIME_DELTA, 3) if not storage: storage = cv.CreateMemStorage(0) seq = cv.SegmentMotion(mhi, segmask, storage, timestamp, MAX_TIME_DELTA) for (area, value, comp_rect) in seq: if comp_rect[2] + comp_rect[3] > 100: # reject very small components color = cv.CV_RGB(255, 0,0) silh_roi = cv.GetSubRect(silh, comp_rect) mhi_roi = cv.GetSubRect(mhi, comp_rect) orient_roi = cv.GetSubRect(orient, comp_rect) mask_roi = cv.GetSubRect(mask, comp_rect) angle = 360 - cv.CalcGlobalOrientation(orient_roi, mask_roi, mhi_roi, timestamp, MHI_DURATION) count = cv.Norm(silh_roi, None, cv.CV_L1, None) # calculate number of points within silhouette ROI if count < (comp_rect[2] * comp_rect[3] * 0.05): continue magnitude = 30. center = ((comp_rect[0] + comp_rect[2] / 2), (comp_rect[1] + comp_rect[3] / 2)) cv.Circle(dst, center, cv.Round(magnitude*1.2), color, 3, cv.CV_AA, 0) cv.Line(dst, center, (cv.Round(center[0] + magnitude * cos(angle * cv.CV_PI / 180)), cv.Round(center[1] - magnitude * sin(angle * cv.CV_PI / 180))), color, 3, cv.CV_AA, 0)
Example #10
Source File: camshift.py From PyCV-time with MIT License | 4 votes |
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) 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: cv.ShowImage( "CamShiftDemo", frame ) else: cv.ShowImage( "CamShiftDemo", backproject) cv.ShowImage( "Histogram", self.hue_histogram_as_image(hist)) c = cv.WaitKey(7) % 0x100 if c == 27: break elif c == ord("b"): backproject_mode = not backproject_mode