def render_with_histogram(img): '''Just a utility to draw a grayscale histogram next to an image.''' _, _, width, height = cv.GetImageROI(img) canvas = cv.CreateImage((width + 200, max(height, 255)), cv.IPL_DEPTH_8U, 1) cv.Rectangle(canvas, (width, 0), (width + 200, height), (0), cv.CV_FILLED) cv.SetImageROI(canvas, (0, 0, width, height)) cv.Copy(img, canvas) cv.SetImageROI(canvas, (width, 0, 200, canvas.height)) hist = cv.CreateHist([255], cv.CV_HIST_ARRAY, [(0,255)], 1) cv.CalcHist([img], hist) values = [cv.QueryHistValue_1D(hist, n) for n in range(255)] max_value = max(values) for n, value in enumerate(values): cv.Rectangle(canvas, (0, n), (int((value / max_value) * 200), n + 1), (255), cv.CV_FILLED) cv.SetImageROI(canvas, (0, 0, canvas.width, canvas.height)) return canvas
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 planes 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 (~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_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 createHist(img): #cv.CvtColor(img,img,cv.CV_BGR2HSV) b_plane = cv.CreateImage((img.width,img.height), 8, 1) g_plane = cv.CreateImage((img.width,img.height), 8, 1) r_plane = cv.CreateImage((img.width,img.height), 8, 1) cv.Split(img,b_plane,g_plane,r_plane,None) planes = [b_plane, g_plane, r_plane] bins = 4 b_bins = bins g_bins = bins r_bins = bins hist_size = [b_bins,g_bins,r_bins] b_range = [0,255] g_range = [0,255] r_range = [0,255] ranges = [b_range,g_range,r_range] hist = cv.CreateHist(hist_size, cv.CV_HIST_ARRAY, ranges, 1) cv.CalcHist([cv.GetImage(i) for i in planes], hist) cv.NormalizeHist(hist,1) return hist
def histogram(src): # Set ccd sampling region. # cv.SetImageROI(src, (10, 10, 100, 100)) # Convert to HSV hsv = cv.CreateImage(cv.GetSize(src), 8, 3) cv.CvtColor(src, hsv, cv.CV_BGR2HSV) s_plane = cv.CreateMat(cv.GetSize(src)[1], cv.GetSize(src)[0], cv.CV_8UC1) h_plane = cv.CreateMat(cv.GetSize(src)[1], cv.GetSize(src)[0], cv.CV_8UC1) cv.Split(hsv, h_plane, s_plane, None, None) planes = [h_plane, s_plane] h_bins = 28 s_bins = 5 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 = 15 # calculate histogram 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) # Reset cv sampling region to full CCD Area # cv.ResetImageROI(src) # plot histogram data 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 hs_histogram(src, mask=None): '''Takes a cvMat and computes a hue-saturation histogram (value is dropped)''' # Convert to HSV # Allocate the 3 HSV 8 bit channels hsv = cv.CreateImage(cv.GetSize(src), 8, 3) # Convert from the default BGR representation to HSV cv.CvtColor(src, hsv, cv.CV_BGR2HSV) # Extract the H and S planes h_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) s_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) v_plane = cv.CreateMat(src.rows, src.cols, cv.CV_8UC1) # Copy out omitting the values we don't want cv.Split(hsv, h_plane, s_plane, v_plane, None) planes = [h_plane, s_plane] if 0: pplane('H plane', h_plane, prefix=' ') pplane('S plane', s_plane, prefix=' ') h_bins = 8 s_bins = 8 #hist_size = [h_bins, s_bins] # hue varies from 0 (~0 deg red) to 180 (~360 deg red again # ??? My values give a hue of 0-255 h_ranges = [0, 255] # saturation varies from 0 (black-gray-white) to # 255 (pure spectrum color) s_ranges = [0, 255] ranges = [h_ranges, s_ranges] # Allocate bins # note that we haven't put any data in yet #1: uniform hist = cv.CreateHist([h_bins, s_bins], cv.CV_HIST_ARRAY, ranges, 1) # Convert the array planes back into images since thats what CalcHist needs? # Doc seems to indcate that cvMat will work as well # Doesn't this cross the hue and saturate values together? That doesn't seem to make sense, like comparing red to blue # Guess its a 2D histogram so it deals with where they overlap cv.CalcHist([cv.GetImage(i) for i in planes], hist, 0, mask) cv.NormalizeHist(hist, 1.0) return hist
def compute_histogram(src, bins=255): hist = cv.CreateHist([255], cv.CV_HIST_ARRAY, ranges=[(0, 256)]) cv.CalcHist([src], hist) #compute histogram cv.NormalizeHist(hist, 1.0) #normalize hist return hist
for i in range(size): intensity = ar[ i] * hpt / maxV #Calculate the intensity to make enter in the image cv.Line(im, (i, size), (i, int(size - intensity)), cv.Scalar(255, 255, 255)) #Draw the line i += 1 #---- Gray image orig = cv.LoadImage("meinv.jpg", cv.CV_8U) histsize = 256 #Because we are working on grayscale pictures which values within 0-255 hist = cv.CreateHist([histsize], cv.CV_HIST_ARRAY, [[0, histsize]], 1) cv.CalcHist([orig], hist) #Calculate histogram for the given grayscale picture histImg = cv.CreateMat( histsize, histsize, cv.CV_8U) #Image that will contain the graph of the repartition of values drawGraph(hist.bins, histImg, histsize) cv.ShowImage("Original Image", orig) cv.ShowImage("Original Histogram", histImg) #--------------------- #---- Equalized image imEq = cv.CloneImage(orig) cv.EqualizeHist(imEq, imEq) #Equlize the original image histEq = cv.CreateHist([histsize], cv.CV_HIST_ARRAY, [[0, histsize]], 1)
import cv2.cv as cv im = cv.LoadImage("meinv.jpg", cv.CV_8U) cv.SetImageROI(im, (1, 1, 30, 30)) histsize = 256 #Because we are working on grayscale pictures hist = cv.CreateHist([histsize], cv.CV_HIST_ARRAY, [[0, histsize]], 1) cv.CalcHist([im], hist) cv.NormalizeHist( hist, 1) # The factor rescale values by multiplying values by the factor _, max_value, _, _ = cv.GetMinMaxHistValue(hist) if max_value == 0: max_value = 1.0 cv.NormalizeHist(hist, 256 / max_value) cv.ResetImageROI(im) res = cv.CreateMat(im.height, im.width, cv.CV_8U) cv.CalcBackProject([im], res, hist) cv.Rectangle(im, (1, 1), (30, 30), (0, 0, 255), 2, cv.CV_FILLED) cv.ShowImage("Original Image", im) cv.ShowImage("BackProjected", res) cv.WaitKey(0)
def HoG(self, tangent, magnitude, writing_method = ""): if writing_method != "": self.writing_method = writing_method if self.image_check(tangent) < 0 or self.image_check(magnitude) < 0: return -1 size = cv.GetSize(tangent) if self.writing_method == "cv": #Create histograms using tangent function. Magnitude is only used as mask #ranges = [(-181,-157,-112,-67,-22,22,67,112,157,181)] ranges= [(0,360)] #ranges = [[0,22,67,112,157,202,247,292,337,360]] hist = cv.CreateHist([8], cv.CV_HIST_ARRAY, ranges,1) cv.CalcHist([tangent],hist,accumulate=0,mask=magnitude) elif self.writing_method == "numpy": #DONT USE THIS: WRONG IMPLEMENTATION ATM #Using magnitude to give ranks/value to tangent picture. Then, the histogram is created tangent_asarray = numpy.asarray(tangent[:,:]) magnitude_asarray = numpy.asarray(magnitude[:,:]) tangent_result = numpy.multiply(tangent_asarray, numpy.divide(magnitude_asarray,255)) normalizedTangent = cv.CreateImageHeader((tangent_result.shape[1], tangent_result.shape[0]), cv.IPL_DEPTH_32F, 1) cv.SetData(normalizedTangent, tangent_result.tostring(), tangent_result.dtype.itemsize * 1 * tangent_result.shape[1]) #ranges = [(-181,-157,-112,-67,-22,22,67,112,157,181)] ranges= [(0,360)] #ranges = [[0,22,67,112,157,202,247,292,337,360]] hist = cv.CreateHist([8], cv.CV_HIST_ARRAY, ranges,1) cv.CalcHist([normalizedTangent],hist,accumulate=0,mask=magnitude) else: #Very slow approach. Use numpy case instead hist = numpy.zeros(8) for x in range(size[0]): for y in range(size[1]): if magnitude[y,x] > 0: if tangent[y,x] < -157.5 or tangent[y,x] > 157.5: hist[0] += 1 *magnitude[y,x]/255 elif tangent[y,x] < -112.5 and tangent[y,x] > -157.5 : hist[7] += 1 *magnitude[y,x]/255 elif tangent[y,x] < -67.5 and tangent[y,x] > -112.5 : hist[6] += 1 *magnitude[y,x]/255 elif tangent[y,x] < -22.5 and tangent[y,x] > -67.5: hist[5] += 1 *magnitude[y,x]/255 elif tangent[y,x] < 22.5 and tangent[y,x] > -22.5: hist[4] += 1 *magnitude[y,x]/255 elif tangent[y,x] < 67.5 and tangent[y,x] > 22.5: hist[3] += 1 *magnitude[y,x]/255 elif tangent[y,x] < 112.5 and tangent[y,x] > 67.5: hist[2] += 1 *magnitude[y,x]/255 elif tangent[y,x] < 157.5 and tangent[y,x] > 112.5: hist[1] += 1 *magnitude[y,x]/255 #sum = numpy.sum(hist) #return numpy.divide(hist,sum) if self.visualize: pass #draw the histogram return hist
#s_plane = cv.Threshold(s_plane, s_plane, minSat, 255, cv.CV_THRESH_BINARY) 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) #SetHistogramm ??? #Normalize im2 = cv.LoadImage("../img/build.png") hsv2 = cv.CreateImage(cv.GetSize(im2), 8, 3) cv.CvtColor(im2, hsv2, cv.CV_BGR2HSV) h_plane2 = cv.CreateImage(cv.GetSize(im2), 8, 1) s_plane2 = cv.CreateImage(cv.GetSize(im2), 8, 1) cv.Split(hsv2, h_plane2, s_plane2, None, None) res = cv.CreateImage((im2.width, im2.height), 8, 3) cv.CalcBackProject([h_plane2, s_plane2], res, hist) cv.MeanShift(res, (10, 10, 30, 30),
def detectObject(filename): img=cv.LoadImage(filename) ''' #get color histogram ''' # im32f=np.zeros((img.shape[:2]),np.uint32) hist_range=[[0,256],[0,256],[0,256]] im32f=cv.CreateImage(cv.GetSize(img), cv2.IPL_DEPTH_32F, 3) cv.ConvertScale(img, im32f) hist=cv.CreateHist([32,32,32],cv.CV_HIST_ARRAY,hist_range,3) ''' #create three histogram''' b=cv.CreateImage(cv.GetSize(im32f), cv2.IPL_DEPTH_32F, 1) g=cv.CreateImage(cv.GetSize(im32f), cv2.IPL_DEPTH_32F, 1) r=cv.CreateImage(cv.GetSize(im32f), cv2.IPL_DEPTH_32F, 1) ''' #create image backproject 32f, 8u ''' backproject32f=cv.CreateImage(cv.GetSize(img),cv2.IPL_DEPTH_32F,1) backproject8u=cv.CreateImage(cv.GetSize(img),cv2.IPL_DEPTH_8U,1) ''' #create binary ''' bw=cv.CreateImage(cv.GetSize(img),cv2.IPL_DEPTH_8U,1) ''' #create kernel image ''' kernel=cv.CreateStructuringElementEx(3, 3, 1, 1, cv2.MORPH_ELLIPSE) cv.Split(im32f, b, g, r,None) planes=[b,g,r] cv.CalcHist(planes, hist) ''' #find min and max histogram bin. ''' minval=maxval=0.0 min_idx=max_idx=0 minval, maxval, min_idx, max_idx=cv.GetMinMaxHistValue(hist) ''' # threshold histogram. this sets the bin values that are below the threshold to zero ''' cv.ThreshHist(hist, maxval/32.0) ''' #backproject the thresholded histogram, backprojection should contian higher values for #background and lower values for the foreground ''' cv.CalcBackProject(planes, backproject32f, hist) ''' #convert to 8u type ''' val_min=val_max=0.0 idx_min=idx_max=0 val_min,val_max,idx_min,idx_max=cv.MinMaxLoc(backproject32f) cv.ConvertScale(backproject32f, backproject8u,255.0/maxval) ''' #threshold backprojected image. this gives us the background ''' cv.Threshold(backproject8u, bw, 10, 255, cv2.THRESH_BINARY) ''' #some morphology on background ''' cv.Dilate(bw, bw,kernel,1) cv.MorphologyEx(bw, bw, None,kernel, cv2.MORPH_CLOSE, 2) ''' #get the foreground ''' cv.SubRS(bw,cv.Scalar(255,255,255),bw) cv.MorphologyEx(bw, bw, None, kernel,cv2.MORPH_OPEN,2) cv.Erode(bw, bw, kernel, 1) ''' #find contours of foreground #Grabcut ''' size=cv.GetSize(bw) color=np.asarray(img[:,:]) fg=np.asarray(bw[:,:]) # mask=cv.CreateMat(size[1], size[0], cv2.CV_8UC1) ''' #Make anywhere black in the grey_image (output from MOG) as likely background #Make anywhere white in the grey_image (output from MOG) as definite foreground ''' rect = (0,0,0,0) mat_mask=np.zeros((size[1],size[0]),dtype='uint8') mat_mask[:,:]=fg mat_mask[mat_mask[:,:] == 0] = 2 mat_mask[mat_mask[:,:] == 255] = 1 ''' #Make containers ''' bgdModel = np.zeros((1, 13 * 5)) fgdModel = np.zeros((1, 13 * 5)) cv2.grabCut(color, mat_mask, rect, bgdModel, fgdModel,cv2.GC_INIT_WITH_MASK) ''' #Multiple new mask by original image to get cut ''' mask2 = np.where((mat_mask==0)|(mat_mask==2),0,1).astype('uint8') gcfg=np.zeros((size[1],size[0]),np.uint8) gcfg=mask2 img_cut = color*mask2[:,:,np.newaxis] contours, hierarchy=cv2.findContours(gcfg ,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) for cnt in contours: print cnt rect_box = cv2.minAreaRect(cnt) box = cv2.cv.BoxPoints(rect_box) box = np.int0(box) cv2.drawContours(color,[box], 0,(0,0,255),2) cv2.imshow('demo', color) cv2.waitKey(0)