def leerPlaca(self, binarizado): """ Args: img: imagen que contiene el texto, obtenida con la funcion cv2.imread. Returns: El string del texto en la imagen. """ cvmat_image = cv.fromarray(binarizado) imgbincv = cv.GetImage(cvmat_image) # GetImage fuente de un error, revisar # Configuración del tesseract api = tesseract.TessBaseAPI() api.Init(".", "eng", tesseract.OEM_DEFAULT) api.SetPageSegMode(tesseract.PSM_AUTO) # Enviando imagen binarizada al tesseract tesseract.SetCvImage(imgbincv, api) text = api.GetUTF8Text() text = re.sub(r'\W+', '', text) return text
def getText(image0): #thicken the border in order to make tesseract feel happy offset = textOffset height, width, channel = image0.shape image1=cv2.copyMakeBorder(image0, offset, offset,offset,offset,\ cv2.BORDER_CONSTANT,value=rgbWhite) #set up tesseract API api = tesseract.TessBaseAPI() api.Init(".", "eng", tesseract.OEM_DEFAULT) api.SetPageSegMode(tesseract.PSM_AUTO) height1, width1, channel1 = image1.shape #star text extraction cvmat_image = cv.fromarray(image1) iplimage = cv.GetImage(cvmat_image) #extract texts tesseract.SetCvImage(iplimage, api) text = api.GetUTF8Text() conf = api.MeanTextConf() return text, conf
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 leerPlaca(self, binarizado): """ Args: img: imagen que contiene el texto, obtenida con la funcion cv2.imread. Returns: El string del texto en la imagen. """ cvmat_image = cv.fromarray(binarizado) imgbincv = cv.GetImage(cvmat_image) # GetImage fuente de un error, revisar # Configuración del tesseract api = tesseract.TessBaseAPI() api.Init('.', 'eng', tesseract.OEM_DEFAULT) api.SetVariable("tessedit_char_whitelist", "0123456789ABCDFGHIJKLMNOPQRSTVWXYZ-UE") api.SetPageSegMode(tesseract.PSM_AUTO) # Enviando imagen binarizada al tesseract tesseract.SetCvImage(imgbincv, api) text = api.GetUTF8Text() text = re.sub(r'\W+', '', text) return text
def transmit_thread(): '''thread for image transmit to GCS''' state = mpstate.camera_state tx_count = 0 skip_count = 0 bsend = block_xmit.BlockSender(0, bandwidth=state.settings.bandwidth, debug=False) state.bsocket = MavSocket(mpstate.mav_master[0]) state.bsend2 = block_xmit.BlockSender(mss=96, sock=state.bsocket, dest_ip='mavlink', dest_port=0, backlog=5, debug=False) state.bsend2.set_bandwidth(state.settings.bandwidth2) while not state.unload.wait(0.02): bsend.tick(packet_count=1000, max_queue=state.settings.maxqueue1) state.bsend2.tick(packet_count=1000, max_queue=state.settings.maxqueue2) check_commands() if state.transmit_queue.empty(): continue (frame_time, regions, im_full, im_640) = state.transmit_queue.get() if state.settings.roll_stabilised: roll=0 else: roll=None pos = get_plane_position(frame_time, roll=roll) # this adds the latlon field to the regions log_joe_position(pos, frame_time, regions) # filter out any regions outside the boundary if state.boundary_polygon: regions = cuav_region.filter_boundary(regions, state.boundary_polygon, pos) regions = cuav_region.filter_regions(im_full, regions, min_score=state.settings.minscore) state.xmit_queue = bsend.sendq_size() state.xmit_queue2 = state.bsend2.sendq_size() state.efficiency = bsend.get_efficiency() state.bandwidth_used = bsend.get_bandwidth_used() state.rtt_estimate = bsend.get_rtt_estimate() jpeg = None if len(regions) > 0: lowscore = 0 highscore = 0 for r in regions: lowscore = min(lowscore, r.score) highscore = max(highscore, r.score) if state.settings.transmit: # send a region message with thumbnails to the ground station thumb = None if state.settings.send1: thumb_img = cuav_mosaic.CompositeThumbnail(cv.GetImage(cv.fromarray(im_full)), regions, thumb_size=state.settings.thumbsize) thumb = scanner.jpeg_compress(numpy.ascontiguousarray(cv.GetMat(thumb_img)), state.settings.quality) pkt = ThumbPacket(frame_time, regions, thumb, state.frame_loss, state.xmit_queue, pos) buf = cPickle.dumps(pkt, cPickle.HIGHEST_PROTOCOL) bsend.set_bandwidth(state.settings.bandwidth) bsend.set_packet_loss(state.settings.packet_loss) bsend.send(buf, dest=(state.settings.gcs_address, state.settings.gcs_view_port), priority=1) # also send thumbnails via 900MHz telemetry if state.settings.send2 and highscore >= state.settings.minscore2: if thumb is None or lowscore < state.settings.minscore2: # remove some of the regions regions = cuav_region.filter_regions(im_full, regions, min_score=state.settings.minscore2) thumb_img = cuav_mosaic.CompositeThumbnail(cv.GetImage(cv.fromarray(im_full)), regions, thumb_size=state.settings.thumbsize) thumb = scanner.jpeg_compress(numpy.ascontiguousarray(cv.GetMat(thumb_img)), state.settings.quality) pkt = ThumbPacket(frame_time, regions, thumb, state.frame_loss, state.xmit_queue, pos) buf = cPickle.dumps(pkt, cPickle.HIGHEST_PROTOCOL) state.bsend2.set_bandwidth(state.settings.bandwidth2) state.bsend2.send(buf, priority=highscore) # Base how many images we send on the send queue size send_frequency = state.xmit_queue // 3 if send_frequency == 0 or (tx_count+skip_count) % send_frequency == 0: jpeg = scanner.jpeg_compress(im_640, state.settings.quality) if jpeg is None: skip_count += 1 continue # keep filtered image size state.jpeg_size = 0.95 * state.jpeg_size + 0.05 * len(jpeg) tx_count += 1 if state.settings.gcs_address is None: continue bsend.set_packet_loss(state.settings.packet_loss) bsend.set_bandwidth(state.settings.bandwidth) pkt = ImagePacket(frame_time, jpeg, state.xmit_queue, pos) str = cPickle.dumps(pkt, cPickle.HIGHEST_PROTOCOL) bsend.send(str, dest=(state.settings.gcs_address, state.settings.gcs_view_port))
from cv2 import cv import numpy as np from pylab import * import glob import os i = 0 for files in glob.glob('D:/pic/car/*.jpg'): filepath,filename = os.path.split(files) image = cv2.imread(filepath + '/' + filename) # image = cv2.imread('D:/pic/car2.jpg') h,w = image.shape[:2] #灰度化 gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) grayIPimage = cv.GetImage(cv.fromarray(gray)) sobel = cv.CreateImage((w, h),cv2.IPL_DEPTH_16S, 1) #创建一张深度为16位有符号(-65536~65535)的的图像区域保持处理结果 cv.Sobel(grayIPimage,sobel,2,0,7) # 进行x方向的sobel检测 temp = cv.CreateImage(cv.GetSize(sobel),cv2.IPL_DEPTH_8U, 1) #图像格式转换回8位深度已进行下一步处理 cv.ConvertScale(sobel, temp,0.00390625, 0) cv.Threshold(temp, temp, 0, 255, cv2.THRESH_OTSU) kernal = cv.CreateStructuringElementEx(3,1, 1, 0, 0) cv.Dilate(temp, temp,kernal,2) cv.Erode(temp, temp,kernal,4) cv.Dilate(temp, temp,kernal,2) # cv.ShowImage('1', temp) kernal = cv.CreateStructuringElementEx(1,3, 0, 1, 0) cv.Erode(temp, temp,kernal,1) cv.Dilate(temp, temp,kernal,3) # cv.ShowImage('2', temp) temp = np.asarray(cv.GetMat(temp))
def writeImage(self): if(self._videoWriter != None): cv.WriteFrame(self._videoWriter, cv.GetImage(self._currentImage))
# print cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_POS_FRAMES) # frame == index # 2. 当前帧frame2赋给frame2gray frame2 = cv.QueryFrame(capture) cv.CvtColor(frame2, frame2gray, cv.CV_RGB2GRAY) # 原图frame2转换为frame2gray格式 # 3. 图像做帧差法,结果为res cv.AbsDiff(frame1gray, frame2gray, res) # 比较frame1gray 和 frame2gray 的差,结果输出给res # res = cv2.absdiff(frame1gray, frame2gray) # 此为cv2新版api cv.ShowImage("After AbsDiff", res) # 4. 保存res作为前景目标foreground # cv.Convert(res, gray) # out_foreground.write(np.uint8(res)) # 保存为前景目标 # out_foreground.write(np.asarray(cv.GetMat(res))) cv.WriteFrame(out_foreground, cv.GetImage(res)) # res格式为cvmat格式,转化为iplimage格式 # 5. 平滑处理 # cv.Smooth(res, res, cv.CV_BLUR, 5, 5) # 光滑一下res # 6. 形态学变换,开闭操作 element = cv.CreateStructuringElementEx(5*2+1, 5*2+1, 5, 5, cv.CV_SHAPE_RECT) # CreateStructuringElementEx(cols, rows, anchorX, anchorY, shape, values=None) cv.MorphologyEx(res, res, None, None, cv.CV_MOP_OPEN) # cv2.morphologyEx(src, op, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) cv.MorphologyEx(res, res, None, None, cv.CV_MOP_CLOSE) # 形态学变换相应的开 闭操作 # 7. 二值化阈值处理:对得到的前景进行阈值选择,去掉伪前景 cv.Threshold(res, res, 10, 255, cv.CV_THRESH_BINARY) # 二值化 cv.Threshold(src, dst, threshold, maxValue, thresholdType) # 8. blob提取电梯区域 ------------- 未完
print image1.dtype.itemsize width_step = width * image1.dtype.itemsize print width_step #method 1 iplimage = cv.CreateImageHeader((width1, height1), cv.IPL_DEPTH_8U, channel1) cv.SetData(iplimage, image1.tostring(), image1.dtype.itemsize * channel1 * (width1)) tesseract.SetCvImage(iplimage, api) text = api.GetUTF8Text() conf = api.MeanTextConf() image = None print "..............." print "Ocred Text: %s" % text print "Cofidence Level: %d %%" % conf #method 2: cvmat_image = cv.fromarray(image1) iplimage = cv.GetImage(cvmat_image) print iplimage tesseract.SetCvImage(iplimage, api) #api.SetImage(m_any,width,height,channel1) text = api.GetUTF8Text() conf = api.MeanTextConf() image = None print "..............." print "Ocred Text: %s" % text print "Cofidence Level: %d %%" % conf api.End()
#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),
pre_y_flag = y_flag if count > 0: y_flag = 1 else: y_flag = 0 if pre_y_flag != y_flag: h_pos.append(y) match_img = [] tmp_mg = [] for i in range(0, leng / 2): cv2.waitKey(3) cv2.rectangle(image, (w_pos[i], h_pos[i]), (w_pos[i + 1], h_pos[i + 1]), (255, 255, 0)) imgIP = cv.GetImage(cv.fromarray(image)) cv.SetImageROI(imgIP, (w_pos[2 * i], h_pos[2 * i], w_pos[2 * i + 1] - w_pos[2 * i], h_pos[2 * i + 1] - h_pos[2 * i])) img_cache = cv.CreateImage( (w_pos[2 * i + 1] - w_pos[2 * i], h_pos[2 * i + 1] - h_pos[2 * i]), cv.IPL_DEPTH_8U, 3) cv.Copy(imgIP, img_cache) match_img.append(img_cache) # cv.SaveImage('ss'+bytes(i)+'.jpg',match_img[i]) cv.ResetImageROI(imgIP) # cv.ShowImage('window'+bytes(i),match_img[i]) #aa=image.copy((range(10,20),range(20,100))) #cv2.imshow("hello",aa) #cv2.imshow("img",image) print w_pos