Esempio n. 1
0
 def use_image(self, im):
     self.kind = 'opencv'
     self.hist = cv.CreateHist([self.bins], cv.CV_HIST_ARRAY,
                               [self.value_range], 1)
     cv.CalcHist([im], self.hist)
     self.min_value, self.max_value, _, _ = cv.GetMinMaxHistValue(self.hist)
     return self
Esempio n. 2
0
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
Esempio n. 3
0
    def run(self):
		
        while True:
            frame = cv.QueryFrame( self.capture)
            
			# Run the cam-shift
            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 )		
	   	    cv.ShowImage("Output",frame)
	   	   	cv.WaitKey(0)
Esempio n. 4
0
def extract_bright(grey_img, histogram=False):
    """
    Extracts brightest part of the image.
    Expected to be the LEDs (provided that there is a dark background)
    Returns a Thresholded image
    histgram defines if we use the hist calculation to find the best margin
    """
    ## Searches for image maximum (brightest pixel)
    # We expect the LEDs to be brighter than the rest of the image
    [minVal, maxVal, minLoc, maxLoc] = cv.MinMaxLoc(grey_img)
    print "Brightest pixel val is %d" % (maxVal)

    #We retrieve only the brightest part of the image
    # Here is use a fixed margin (80%), but you can use hist to enhance this one
    if 0:
        ## Histogram may be used to wisely define the margin
        # We expect a huge spike corresponding to the mean of the background
        # and another smaller spike of bright values (the LEDs)
        hist = grey_histogram(img, nBins=64)
        [hminValue, hmaxValue, hminIdx, hmaxIdx] = cv.GetMinMaxHistValue(hist)
        margin = 0  # statistics to be calculated using hist data
    else:
        margin = 0.8

    thresh = int(maxVal * margin)  # in pix value to be extracted
    print "Threshold is defined as %d" % (thresh)

    thresh_img = cv.CreateImage(cv.GetSize(img), img.depth, 1)
    cv.Threshold(grey_img, thresh_img, thresh, 255, cv.CV_THRESH_BINARY)

    return thresh_img
Esempio n. 5
0
def normalize_plane(plane, aggressive=0):
    if aggressive:
        #        smooth = image_empty_clone(plane)
        #        cv.Smooth(plane, smooth, cv.CV_GAUSSIAN, 13, 13)
        hist = get_gray_histogram(plane, bins=255)
        _, max_value, _, max_color = cv.GetMinMaxHistValue(hist)
        thr_value = max_value * aggressive
        down_threshold, up_threshold = None, None
        for k in range(256):
            down_val = cv.QueryHistValue_1D(hist, k)
            up_val = cv.QueryHistValue_1D(hist, 254 - k)
            if down_threshold is None and down_val >= thr_value:
                down_threshold = k
            if up_threshold is None and up_val >= thr_value:
                up_threshold = k
            if down_threshold is not None and up_threshold is not None:
                break

        sub_plane = None
        if down_threshold > 0:
            sub_plane = image_empty_clone(plane)
            cv.SubS(plane, down_threshold, sub_plane)

        add_plane = None
        if down_threshold + up_threshold > 0:
            add_plane = image_empty_clone(plane)
            cv.AddS(sub_plane or plane, down_threshold + up_threshold,
                    add_plane)
        plane = add_plane or plane
    norm_plane = image_empty_clone(plane)
    cv.Normalize(plane, norm_plane, 0, 255, cv.CV_MINMAX)
    return norm_plane
Esempio n. 6
0
    def renderIndividualHist(self, painting):
        height = 500
        width = 500
        (r, g, b) = cv.GetDims(painting.data.bins)

        (_, tallest, _, _) = cv.GetMinMaxHistValue(painting.data)
        scale = height / tallest
        if scale > 1:
            scale = 1

        histImage = cv.CreateImage((width, height), 8, 3)
        prev = -1

        cv.Rectangle(histImage, (0, 0), (width, height), cv.CV_RGB(0, 0, 0),
                     cv.CV_FILLED)

        step = width / (r * g * b)
        cur = 0
        for (i, j, k) in iter3D(range(r), range(g), range(b)):
            intensity = cv.QueryHistValue_3D(painting.data, i, j, k)
            color = cv.CV_RGB(int((255 / r) * i), int((255 / g) * j),
                              int((255 / b) * k))
            x1 = cur
            cur += step
            x2 = x1 + step
            cv.Rectangle(histImage, (int(x1), height),
                         (int(x2), height - int(intensity * scale)), color,
                         cv.CV_FILLED)

        windowTitle = '{0} ({1})'.format(painting.title, painting.year)
        cv.NamedWindow(windowTitle)
        cv.ShowImage(windowTitle, histImage)
        cv.WaitKey(0)
Esempio n. 7
0
 def rescaleMax(self,value=255):
     ''' 
     Rescale the histogram such that the maximum equals the value.
     '''
     cv.NormalizeHist(self.hist,1)
     _,max_value,_,_ = cv.GetMinMaxHistValue(self.hist)
     if max_value == 0:
         max_value = 1.0
     cv.NormalizeHist(self.hist,255/max_value)
Esempio n. 8
0
    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 calcularHistograma(self):
        cv.CalcArrHist([self.imagem_destino], self.histograma)
        valorMaximoMinimo = cv.GetMinMaxHistValue(self.histograma)
        cv.Rectangle(self.imagemHistograma, (0, 0), (512, 100),
                     cv.CV_RGB(0, 0, 0), -1)

        for i in range(512):
            valor = cv.QueryHistValue_1D(self.histograma, i)
            normalizado = cv.Round(valor * 100 / valorMaximoMinimo[1])
            cv.Line(self.imagemHistograma, (i, 100), (i, 100 - normalizado),
                    cv.CV_RGB(255, 255, 255))
Esempio n. 10
0
    def do_camshift(self, cv_image):
        """ Get the image size """
        image_size = cv.GetSize(cv_image)
        image_width = image_size[0]
        image_height = image_size[1]
        
        """ Convert to HSV and keep the hue """
        hsv = cv.CreateImage(image_size, 8, 3)
        cv.CvtColor(cv_image, hsv, cv.CV_BGR2HSV)
        self.hue = cv.CreateImage(image_size, 8, 1)
        cv.Split(hsv, self.hue, None, None, None)

        """ Compute back projection """
        backproject = cv.CreateImage(image_size, 8, 1)

        """ Run the cam-shift algorithm """
        cv.CalcArrBackProject( [self.hue], backproject, self.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(cv_image, self.selection)
            save = cv.CloneMat(sub)
            cv.ConvertScale(cv_image, cv_image, 0.5)
            cv.Copy(save, sub)
            x,y,w,h = self.selection
            cv.Rectangle(cv_image, (x,y), (x+w,y+h), (255,255,255))

            sel = cv.GetSubRect(self.hue, self.selection )
            cv.CalcArrHist( [sel], self.hist, 0)
            (_, max_val, _, _) = cv.GetMinMaxHistValue(self.hist)
            if max_val != 0:
                cv.ConvertScale(self.hist.bins, self.hist.bins, 255. / max_val)
        elif self.track_window and is_rect_nonzero(self.track_window):
            cv.EllipseBox( cv_image, track_box, cv.CV_RGB(255,0,0), 3, cv.CV_AA, 0 )
            
            roi = RegionOfInterest()
            roi.x_offset = int(min(image_width, max(0, track_box[0][0] - track_box[1][0] / 2)))
            roi.y_offset = int(min(image_height, max(0, track_box[0][1] - track_box[1][1] / 2)))
            roi.width = int(track_box[1][0])
            roi.height = int(track_box[1][1])
            self.ROI.publish(roi)

        cv.ShowImage("Histogram", self.hue_histogram_as_image(self.hist))
        
        if not self.backproject_mode:
            return cv_image
        else:
            return backproject
Esempio n. 11
0
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
Esempio n. 12
0
def histogram_image(hist, color=(255, 255, 255), background_color=(0, 0, 0), num_bins=256):
    '''Returns an image displaying the the given histogram.'''

    max_value = int(cv.GetMinMaxHistValue(hist)[1])

    img = cv.CreateImage((num_bins, num_bins), 8, 3)
    cv.Set(img, background_color)

    for i in xrange(num_bins):
        height = int(cv.QueryHistValue_1D(hist, i) / max_value * num_bins)
        cv.Line(img, (i, num_bins), (i, num_bins - height), color)

    return img
Esempio n. 13
0
def pitch_detect(intrinsics, dist_coeffs, dst0):
    capture = cv.CaptureFromCAM(0)
    src = cv.QueryFrame(capture)
    cv.SetImageROI(dst0, (0, 0, 640, 480))
    cv.Undistort2(src, dst0, intrinsics, dist_coeffs)
    cv.SetImageROI(dst0, image_ROI)
    dst = GetImage(dst0)
    hsv = cv.CreateImage(size, 8, 3)
    CvtColor(dst, hsv, CV_RGB2HSV)
    cv.Split(hsv, hue, sat, val, None)
    hist = cv.CreateHist([32, 64], CV_HIST_ARRAY, [[0, 180], [0, 256]], 1)
    cv.CalcHist([hue, sat], hist, 0, None)
    values = cv.GetMinMaxHistValue(hist)
    tweak = values[3][0]
    return tweak
Esempio n. 14
0
    def channel_to_image(self, chan, scale_x=3, scale_y=3, y_range=64):
        """
        Creates an iplimage displaying histogram results after its computation
        """
        if ((type(chan) != int) or (chan <= 0)):
            raise TypeError("(%s) Positive integer expected!" %
                            (sys._getframe().f_code.co_name))
        elif chan > self.channels:
            raise ValueError("(%s) Incoherent channel selected!" %
                             (sys._getframe().f_code.co_name))

        if ((type(scale_x) != int) or (scale_x <= 0)):
            raise TypeError("(%s) Positive integer expected!" %
                            (sys._getframe().f_code.co_name))

        if ((type(scale_y) != int) or (scale_y <= 0)):
            raise TypeError("(%s) Positive integer expected!" %
                            (sys._getframe().f_code.co_name))

        if ((type(y_range) != int) or (y_range <= 0)):
            raise TypeError("(%s) Positive integer expected!" %
                            (sys._getframe().f_code.co_name))

        max_range = self.ranges[1] - 1
        (_, hist_max, _, _) = cv.GetMinMaxHistValue(self.cvhist[chan - 1])
        hist_img = cv.CreateImage((max_range * scale_x, y_range * scale_y),
                                  self.depth, 1)
        cv.Zero(hist_img)  # resets image values

        for i in range(max_range):  # 0 to max_range
            hist_value = cv.QueryHistValue_1D(self.cvhist[chan - 1], i)
            next_value = cv.QueryHistValue_1D(self.cvhist[chan - 1], i + 1)
            pt1 = (int(i * scale_x), int(y_range * scale_y))
            pt2 = (int(i * scale_x + scale_x), int(y_range * scale_y))
            pt3 = (int(i * scale_x + scale_x),
                   int((y_range - (next_value * y_range / hist_max)) *
                       scale_y))
            pt4 = (int(i * scale_x),
                   int((y_range - (hist_value * y_range / hist_max)) *
                       scale_y))
            pts = (pt1, pt2, pt3, pt4)
            cv.FillConvexPoly(hist_img, pts, 255, lineType=8, shift=0)

        return hist_img
Esempio n. 15
0
    def update(self, im=None):
        if im is not None:
            self.im = im

        cv.CalcArrHist([self.im], self.hist)
        (min_value, max_value, _, _) = cv.GetMinMaxHistValue(self.hist)
        cv.Scale(self.hist.bins, self.hist.bins,
                 float(self.hist_image.height) / max_value, 0)

        cv.Set(self.hist_image, cv.ScalarAll(255))
        bin_w = round(float(self.hist_image.width) / self.hist_size)

        for i in range(self.hist_size):
            cv.Rectangle(self.hist_image,
                         (int(i * bin_w), self.hist_image.height),
                         (int((i + 1) * bin_w), self.hist_image.height -
                          cv.Round(self.hist.bins[i])), self.color, -1, 8, 0)

        cv.ShowImage(self.title, self.hist_image)
Esempio n. 16
0
    def set_hist(self, frame, selection):
        sub = cv.GetSubRect(frame, selection)
        save = cv.CloneMat(sub)

        cv.ConvertScale(frame, frame, 0.5)
        cv.Copy(save, sub)
        x, y, w, h = selection

        # rectangular piece of frame
        cv.Rectangle(frame, (x, y), (x + w, y + h), (255, 255, 255))

        sel = cv.GetSubRect(self.hue, selection)
        cv.CalcArrHist([sel], self.hist, 0)

        # get the most prevalent color in the histogram
        (_, max_val, _, _) = cv.GetMinMaxHistValue(self.hist)

        if max_val != 0:
            cv.ConvertScale(self.hist.bins, self.hist.bins, 255. / max_val)
            print "Val set to " + str(max_val)
Esempio n. 17
0
def get_2d_hist_img(x_bins=30, y_bins=32, scale=10, hist=None, img=None):
    if not hist:
        if img:
            hist = get_hs_2d_hist(img, x_bins, y_bins)
        else:
            raise Exception("Histogram or image should be given")

    (_, max_value, _, _) = cv.GetMinMaxHistValue(hist)

    hist_img = cv.CreateImage((x_bins * scale, y_bins * scale), 8, 3)

    for h in range(x_bins):
        for s in range(y_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(255 - intensity, 255 - intensity,
                       255 - intensity), cv.CV_FILLED)
    return hist_img
Esempio n. 18
0
def get_hist_image(hist, bins, width=500):
    height = 255
    white = cv.RGB(255, 255, 255)
    black = cv.RGB(0, 0, 0)

    img_size = (width, height)
    hist_img = cv.CreateImage(img_size, 8, 1)

    cv.Rectangle(hist_img, (0, 0), img_size, white, cv.CV_FILLED)

    (_, max_value, _, _) = cv.GetMinMaxHistValue(hist)

    scale = width / float(bins)
    x = 0
    for s in range(bins):
        bin_val = cv.QueryHistValue_1D(hist, s)
        y = cv.Round(bin_val * height / max_value)
        cv.Rectangle(hist_img, (x, height - y), (x + scale, height), black,
                     cv.CV_FILLED)
        x += scale
    return hist_img
Esempio n. 19
0
def OCVHistogram(frame, ranges=[[0, 256]], hist_size=64):
    """Create a histogram of given frame"""
    if frame.nChannels != 1:
        dest = OCVCopyGrayscale(frame)
    else:
        dest = frame

    hist_image = cv.CreateImage((dest.width, dest.height), 8, 1)
    hist = cv.CreateHist([hist_size], cv.CV_HIST_ARRAY, ranges, 1)

    cv.CalcArrHist([dest], hist)
    (min_value, max_value, _, _) = cv.GetMinMaxHistValue(hist)
    cv.Scale(hist.bins, hist.bins, float(hist_image.height) / max_value, 0)

    cv.Set(hist_image, cv.ScalarAll(255))
    bin_w = round(float(hist_image.width) / hist_size)

    for i in range(hist_size):
        cv.Rectangle(hist_image, (int(i * bin_w), hist_image.height), (int(
            (i + 1) * bin_w), hist_image.height - cv.Round(hist.bins[i])),
                     cv.ScalarAll(0), -1, 8, 0)

    return hist_image
Esempio n. 20
0
	def __init__(self):
		self.bridge = cv_bridge.CvBridge()

		# Histogram parameters.
		bins_hue = rospy.get_param('~bins_hue', 10)
		min_hue  = rospy.get_param('~min_hue', 0)
		max_hue  = rospy.get_param('~max_hue', 255)

		bins_sat = rospy.get_param('~bins_sat', 10)
		min_sat  = rospy.get_param('~min_sat', 0)
		max_sat  = rospy.get_param('~max_sat', 255)
		self.ker_size = rospy.get_param('~kernel_size', 3)

		self.bins     = [ bins_hue, bins_sat ]
		self.ranges   = [ (min_hue, max_hue), (min_sat, max_sat) ]
		self.channels = [ CH_HUE, CH_SAT, CH_VAL ]

		# Histogram matching parameters.
		width  = rospy.get_param('~window_width',  20)
		height = rospy.get_param('~window_height', 20)
		self.window = (width, height)
		self.method = cv.CV_COMP_CORREL

		# Load training data from a CSV file.
		train_path = rospy.get_param('~train_path')
		train_data = numpy.genfromtxt(train_path, delimiter=',', comments='@', dtype=numpy.uint8)

		# Build the HS-histogram of both positive and negative examples.
		pos = train_data[train_data[:,CH_LABEL] == 1, :]
		self.hist_pos = self.LoadHistogram(pos, self.channels, self.bins, self.ranges, 1.0)

		val_min, val_max, loc_min, loc_max = cv.GetMinMaxHistValue(self.hist_pos)
		rospy.loginfo('histogram counts are in range [{0}, {1}]'.format(val_min, val_max))

		self.sub = rospy.Subscriber('image', sensor_msgs.msg.Image, self.ImageCallback)
		self.pub = rospy.Publisher('white', sensor_msgs.msg.Image)
    def histogram(self, src):
	# Convert to HSV
    	hsv = cv.CreateImage(cv.GetSize(src), 8, 3)
    	cv.CvtColor(src, hsv, cv.CV_BGR2HSV)
	h_plane = cv.CreateMat(cv.GetSize(src)[1], cv.GetSize(src)[0], cv.CV_8UC1)
	s_plane = cv.CreateMat(cv.GetSize(src)[1], cv.GetSize(src)[0], cv.CV_8UC1)
	v_plane = cv.CreateMat(cv.GetSize(src)[1], cv.GetSize(src)[0], cv.CV_8UC1)
	cv.Split(hsv, h_plane, s_plane, v_plane, None)
        planes = [h_plane, s_plane]
        h_bins = 30
        s_bins = 32
        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]
        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_val = cv.GetMinMaxHistValue(hist)[3]
	max_hue_bin = max_val[0]
	max_sat_bin = max_val[1]
	
	# display this color in RGB
	h_interval = 6
	s_interval = 8
	hue = h_interval * max_hue_bin
	BGR_color = HSV_to_RGB(hue)
	cv.NamedWindow("About this color?", cv.CV_WINDOW_AUTOSIZE)
        cv.MoveWindow("About this color?", 620, 530)
        color_swatch = cv.CreateImage((200, 140), 8, 3)
        cv.Set(color_swatch, BGR_color)
        cv.ShowImage("About this color?", color_swatch)
	return BGR_color, hue
                ###################################
                ########calculate histogram########
                if biggestFace > 0:
                    #sets the Region of Interest (face) in HSV image
                    cv.SetImageROI(frameSmallHSV, rect)
                    face = cv.CreateImage(cv.GetSize(frameSmallHSV),
                                          frameSmallHSV.depth,
                                          frameSmallHSV.nChannels)
                    cv.Copy(frameSmallHSV, face)
                    cv.ResetImageROI(frameSmallHSV)
                    #get size of face area
                    faceArea = face.height * face.width

                    hist = hs_histogram(face)
                    myHist = hist.getHist(hBins, sBins)
                    (_, maxValue, _, _) = cv.GetMinMaxHistValue(myHist)
                    print "face detected >>> Histogram constructed"
                    hasHist = True
                ###################################

            else:  # if hist already calculated
                skinProbImg = cv.CreateImage(cv.GetSize(frameSmallHSV), 8, 1)
                hueImg = cv.CreateMat(frameSmallHSV.height,
                                      frameSmallHSV.width, cv.CV_8UC1)
                satImg = cv.CreateMat(frameSmallHSV.height,
                                      frameSmallHSV.width, cv.CV_8UC1)
                cv.Split(frameSmallHSV, hueImg, satImg, None, None)

                binSum = face.height * face.width
                maxProbInt = 0
                for x in range(0, frameSmallHSV.height):
Esempio n. 23
0
 def recompute_histogram(self, hist):
     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)
Esempio n. 24
0
def calc_1dhisto(inframe,
                 nbin=256,
                 scale=2,
                 histh=200,
                 hist=None,
                 histimg=None):
    """
	Calculate 1D intensity histogram of a iplimage, and return the histogram 
	itself (as cv2.cv.cvhistogram) and an image representing this histogram (
	as 8bit unsigned iplimage) Use **hist** and **histimg** if they are 
	provided, otherwise create them from scratch.

	To re-use the allocated memory, simply pass the output as input for 
	input **hist** and **histimg**.

	Histogram bar height is calculated as follows:
		
		bin_height = bin_count*1.0/(npix/nbin) * 0.2 * histh

	where bin_height is in pixels, bin_count is the number of pixels in this 
	bin, npix the total number of pixels in the image, nbin the total bins, 
	such that (npix/nbin) is the average bin count. 0.2 is a factor that 
	sets the average bin height to 20% and histh scales the bin normalized 
	bin height to pixels.

	@param [in] inframe Input frame, as iplimage
	@param [in] nbin Number of intensity bins
	@param [in] scale Histogram image bar width in pixels
	@param [in] histh Histogram image height in pixels
	@param [in] hist Previously allocated cv2.cv.cvhistogram to use
	@param [in] histimg Previously allocated iplimage to use
	@return Tuple of (histogram, histogram image)
	"""

    if (inframe.depth == cv.IPL_DEPTH_32F):
        hranges = [[0, 1]]
    elif (inframe.depth in [
            cv.IPL_DEPTH_8U, cv.IPL_DEPTH_8S, cv.IPL_DEPTH_16U,
            cv.IPL_DEPTH_16S, cv.IPL_DEPTH_32S
    ]):
        hranges = None
    else:
        raise ValueError("Unsupported datatype ('%s')" % (str(inframe.depth)))

    if (not hist):
        hist = cv.CreateHist([nbin],
                             cv.CV_HIST_ARRAY,
                             ranges=[[0, 1]],
                             uniform=1)
    if (not histimg):
        histimg = cv.CreateImage((nbin * scale, histh), cv.IPL_DEPTH_8U, 1)

    npix = np.product(cv.GetDims(inframe))

    cv.CalcHist([cv.GetImage(inframe)], hist)
    hmin, hmax, _, _ = cv.GetMinMaxHistValue(hist)

    cv.Zero(histimg)
    for i in range(nbin):
        bin_val = cv.QueryHistValue_1D(hist, i)
        bin_h = int(bin_val * 1.0 / (npix / nbin) * 0.2 * histh)
        cv.Rectangle(histimg, (i * scale, histh),
                     ((i + 1) * scale - 1, histh - bin_h), 256, cv.CV_FILLED)

    return hist, histimg
Esempio n. 25
0
    #	Build the histogram and compute its contents.

    hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, [(0, 180),
                                                              (0, 255)], 1)
    cv.CalcHist(planes, hist, 0, None)
    cv.NormalizeHist(hist, 1.0)

    #	Create an image to use to visualize our histogram.

    scale = 10
    hist_img = cv.CreateImage((h_bins * scale, s_bins * scale), 8, 3)
    cv.Zero(hist_img)

    max_value = 0
    (minvalue, maxvalue, minidx, maxidx) = cv.GetMinMaxHistValue(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 / maxvalue)
            cv.Rectangle(hist_img, (h * scale, s * scale),
                         ((h + 1) * scale - 1, (s + 1) * scale - 1),
                         (intensity, intensity, intensity), cv.CV_FILLED)
            print intensity

    cv.ShowImage("src", src)
    cv.ShowImage("hsv", hist_img)
    cv.WaitKey(0)

else:
Esempio n. 26
0
def back_project_hs(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)
    cv.CvtColor(patch, hsv_patch, cv.CV_BGR2HSV)

    # 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]
    # planes = [s_plane_patch]#, s_plane, v_plane]

    h_bins = 30
    s_bins = 32
    hist_size = [h_bins, s_bins]
    # hue varies from 0 (~0 deg red) to 180 (~360 deg red again */
    h_ranges = [0, 180]
    s_ranges = [0, 255]
    # saturation varies from 0 (black-gray-white) to
    # 255 (pure spectrum color)
    ranges = [h_ranges, s_ranges]#, s_ranges, v_ranges]
    #ranges = [s_ranges]#, s_ranges, v_ranges]
    scale = 1
    hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1)
    #hist = cv.CreateHist([s_bins], cv.CV_HIST_ARRAY, ranges, 1)
    cv.CalcHist([cv.GetImage(i) for i in planes], hist)
    
    (min_value, max_value, _, _) = cv.GetMinMaxHistValue(hist)

    #cv.NormalizeHist(hist, 20*250.0)
    print "min hist value is :", min_value
    print "max hist value is :", max_value

    back_proj_img = cv.CreateImage(cv.GetSize(src), 8, 1)

    #cv.NormalizeHist(hist, 2000)

    cv.CalcBackProject([h_plane_img, s_plane_img], back_proj_img, hist)
    back_modified = cv.CreateImage(cv.GetSize(src), 8, 1)
    back_modified2 = cv.CreateImage(cv.GetSize(src), 8, 1)

    # cv.Dilate(back_proj_img, back_proj_img)
    # cv.Erode(back_proj_img, back_proj_img)
    #cv.Smooth(back_proj_img, back_modified)
    
    #cv.AdaptiveThreshold(back_proj_img, back_modified, 255, adaptive_method=cv.CV_ADAPTIVE_THRESH_GAUSSIAN_C)
    #cv.Threshold(back_proj_img, back_modified, 250, 255, cv.CV_THRESH_BINARY)
    #cv.MorphologyEx(back_modified,back_modified2, None, None, cv.CV_MOP_CLOSE, 3)
    #cv.MorphologyEx(back_modified,back_modified2, None, None, cv.CV_MOP_CLOSE, 1)    
    # cv.MorphologyEx(back_proj_img,back_modified2, None, None, cv.CV_MOP_CLOSE, 1)
    #cv.MorphologyEx(back_modified2,back_modified2, None, None, cv.CV_MOP_OPEN, 2)    




    cv.MorphologyEx(back_proj_img,back_modified, None, None, cv.CV_MOP_OPEN, 1)
    cv.MorphologyEx(back_modified,back_modified, None, None, cv.CV_MOP_CLOSE, 2)    
    cv.Threshold(back_modified, back_modified, 250, 255, cv.CV_THRESH_BINARY)

    # cv.MorphologyEx(back_proj_img,back_modified2, None, None, cv.CV_MOP_CLOSE, 1)
    # cv.MorphologyEx(back_modified2,back_modified2, None, None, cv.CV_MOP_OPEN, 2)    




    #cv.FloodFill(back_modified, (320, 240), cv.Scalar(255), cv.Scalar(30), cv.Scalar(30), flags=8)

    # for i in xrange (10):
    #     cv.MorphologyEx(back_modified,back_modified, None, None, cv.CV_MOP_OPEN, 3)
    #     cv.MorphologyEx(back_modified,back_modified, None, None, cv.CV_MOP_CLOSE, 1)    

    
    #cv.SubRS(back_modified, 255, back_modified)

    # cv.CalcBackProject([s_plane_img], back_proj_img, hist)
    # cv.Scale(back_proj_img, back_proj_img, 30000)
    cv.ShowImage("back_projection", back_proj_img)
    cv.ShowImage("back_modified", back_modified)
    cv.ShowImage("back_modified2", back_modified2)

    cv.WaitKey(0)


    
    #return back_proj_img, hist
    return back_modified, hist
Esempio n. 27
0
 def getMinMax(self):
     (min, max, minIdx, maxIdx) = cv.GetMinMaxHistValue(self.hist)
     return maxIdx
def camshift_track(roi_selection, img):
    """ setup_camshift:
        - is_rect_nonzero(r)
        - hue_histogram_as_image(hist)
        - making_selection(roi_selection)
        - create_hist()
        - placeholder()

    compute_camshift_centroid:
        - isolate_hue
        - compute_back_projection
        - recompute_histogram(roi_selection)
        - run_camshift
        - draw ellipse
        - find_centroid(track_box)
        - show_hist
"""

    # setup_camshift
    def is_rect_nonzero(r):
        (_, _, w, h) = r
        return (w > 0) and (h > 0)

    def hue_histogram_as_image(hist):
        """ Returns 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

# making_selection
# print roi_selection[0]
# print roi_selection[1]

    point1 = roi_selection[0]
    point2 = roi_selection[1]
    xmin = point1[0]
    ymin = point1[1]
    xmax = point2[0]
    ymax = point2[1]
    widthx = xmax - xmin
    heighty = ymax - ymin

    selection = (xmin, ymin, widthx, heighty)
    # end of making_selection

    # create_hist()
    hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0, 180)], 1)
    # end of create_hist

    # placeholder()
    backproject_mode = False
    # end of placeholder

    while True:
        # capture = cv.CaptureFromCAM(0)
        # frame = cv.QueryFrame(capture)

        # isolate_hue
        # Convert to HSV and keep the hue
        hsv = cv.CreateImage(cv.GetSize(frame), 8, 3)
        cv.CvtColor(frame, hsv, cv.CV_BGR2HSV)
        hue = cv.CreateImage(cv.GetSize(frame), 8, 1)
        cv.Split(hsv, hue, None, None, None)
        # end isolate_hue

        # Compute back projection
        backproject = cv.CreateImage(cv.GetSize(frame), 8, 1)
        # end compute back projection

        # highlight the current selected rectangle and recompute the histogram
        w = xmax - xmin
        h = ymax - ymax
        xmin, ymin, w, h = selection
        cv.Rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 0, 255))
        # end highlight

        # Run the camshift
        cv.CalcArrBackProject([hue], backproject, hist)
        # end run camshift

        # draw ellipse
        print "selection is" + str(selection)
        if selection and is_rect_nonzero(selection):
            crit = (cv.CV_TERMCRIT_EPS | cv.CV_TERMCRIT_ITER, 10, 1)
            (iters, (area, value, rect),
             track_box) = cv.CamShift(backproject, selection, crit)
            selection = rect

            sel = cv.GetSubRect(hue, 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 selection and is_rect_nonzero(selection):
            cv.EllipseBox(frame, track_box, cv.CV_RGB(255, 0, 0), 3, cv.CV_AA,
                          0)
            # end draw ellipse

            # find centroid coordinate (x,y) and area (z)
            selection_centroid = track_box[0]
            xposition = selection_centroid[0]
            yposition = selection_centroid[1]
            width_height = track_box[1]
            # selection_area = width_height[0]*width_height[1]
            face_centroid_camshift = (xposition, yposition)
            # return face_centroid_camshift
# end find centroid

# show hist
        if backproject_mode:
            # cv.ShowImage( "CamShiftDemo", backproject)
            cv.ShowImage("Histogram", self.hue_histogram_as_image(hist))


# end show hist

    print "I made it this far"
    # print face_centroid_camshift
    return face_centroid_camshift
Esempio n. 29
0
    def OnIdle( self, ):
        """Request refresh of the context whenever idle.
        track, get position, update camera, then redraw"""
        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), (0,0,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 )

# find centroid coordinate (x,y) and area (z)
                selection_centroid = track_box[0]
                global xposition
                xposition = selection_centroid[0]
                global yposition
                yposition = selection_centroid[1]
                width_height = track_box[1]


# writes output of coordinates to seed file if needed
                # with open('seed.txt', 'a') as f:
                #     value = (xposition, yposition)
                #     s = str(value) + '\n'
                #     f.write(s)
                #     # f.write('end_of_session')
                # f.close()

# print outs
                print "x: " + str(xposition)
                print "y: " + str(yposition)
                selection_area = width_height[0]*width_height[1]
                # print "The width is: " + str(width_height[0]) + " The height is: " + str(width_height[1])
                # print "centroid is: " + str(selection_centroid)
                # return "centroid is: " + str(selection_centroid)
                print "area: " + str(selection_area)
                # return "area is: " + str(selection_area)

            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(10)
            if c == 27: # escape key
                break
            elif c == ord("b"): # show backproject mode with "b" key
                backproject_mode = not backproject_mode

        self.triggerRedraw(1)        
        return 1