Exemple #1
0
def url_jpg_contours(url):
    position = 100
    filedata = urllib2.urlopen(url).read()
    imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1)
    cv.SetData(imagefiledata, filedata, len(filedata))
    im = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR)
    col_edge = cv.CreateImage((im.width, im.height), 8, 3)

    # convert to grayscale
    gray_im = cv.CreateImage((im.width, im.height), 8, 1)
    edge_im = cv.CreateImage((im.width, im.height), 8, 1)
    cv.CvtColor(im, gray_im, cv.CV_BGR2GRAY)
    cv.Canny(gray_im, edge_im, position, position * 3, 3)
    cv.SetZero(col_edge)
    # copy edge points
    cv.Copy(im, col_edge, edge_im)
    edge_im_array = np.asarray(edge_im[:])

    ret, edge_im_array = cv2.threshold(edge_im_array, 127, 255,
                                       cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(edge_im_array, cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_SIMPLE)
    scale = 10000.0
    points = []
    for contour in contours:
        for i in contour:
            for j in i:
                lng_offset = j[0] / scale
                lat_offset = j[1] / scale
                points.append([lng_offset, lat_offset])
    return points
Exemple #2
0
def load_sample(name=None):
    if len(argv) > 1:
        img0 = cv.LoadImage(argv[1], cv.CV_LOAD_IMAGE_COLOR)
    elif name is not None:
        try:
            img0 = cv.LoadImage(name, cv.CV_LOAD_IMAGE_COLOR)
        except IOError:
            urlbase = 'http://code.opencv.org/projects/opencv/repository/revisions/master/raw/samples/c/'
            file = name.split('/')[-1]
            filedata = urllib2.urlopen(urlbase + file).read()
            imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1)
            cv.SetData(imagefiledata, filedata, len(filedata))
            img0 = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR)
    return img0
def load_sample(name=None):
    if len(argv) > 1:
        img0 = cv.LoadImage(argv[1], cv.CV_LOAD_IMAGE_COLOR)
    elif name is not None:
        try:
            img0 = cv.LoadImage(name, cv.CV_LOAD_IMAGE_COLOR)
        except IOError:
            urlbase = 'https://raw.github.com/Itseez/opencv/master/samples/c/'
            file = name.split('/')[-1]
            filedata = urllib2.urlopen(urlbase + file).read()
            imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1)
            cv.SetData(imagefiledata, filedata, len(filedata))
            img0 = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR)
    return img0
Exemple #4
0
def mp_icon(filename):
    '''load an icon from the data directory'''
    # we have to jump through a lot of hoops to get an OpenCV image
    # when we may be in a package zip file
    try:
        import pkg_resources
        raw = pkg_resources.resource_stream(__name__,
                                            "data/%s" % filename).read()
    except Exception:
        raw = open(os.path.join(__file__, 'data', filename)).read()
    imagefiledata = cv.CreateMatHeader(1, len(raw), cv.CV_8UC1)
    cv.SetData(imagefiledata, raw, len(raw))
    img = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR)
    return img
Exemple #5
0
    def convert_tif_to_png(self, buffer):
        if not cv:
            msg = """[PILEngine] convert_tif_to_png failed: opencv not imported"""
            logger.error(msg)
            return buffer

        # can not use cv2 here, because ubuntu precise shipped with python-opencv 2.3 which has bug with imencode
        # requires 3rd parameter buf which could not be created in python. Could be replaced with these lines:
        # img = cv2.imdecode(numpy.fromstring(buffer, dtype='uint16'), -1)
        # buffer = cv2.imencode('.png', img)[1].tostring()
        mat_data = cv.CreateMatHeader(1, len(buffer), cv.CV_8UC1)
        cv.SetData(mat_data, buffer, len(buffer))
        img = cv.DecodeImage(mat_data, -1)
        buffer = cv.EncodeImage(".png", img).tostring()

        mime = self.get_mimetype(buffer)
        self.extension = EXTENSION.get(mime, '.jpg')
        return buffer
Exemple #6
0
 def __showPic(self):
     try:
         import cv2.cv as cv
     except:
         return "Need OpenCV"
     content = urllib2.urlopen(
         "http://www.renren.com/validateuser.do").read()
     place = content.find("http://icode.renren.com/getcode.do?t=ninki&rnd=")
     place2 = content.find("\"/>", place)
     content = content[place:place2]
     filedata = urllib2.urlopen(content).read()
     imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1)
     cv.SetData(imagefiledata, filedata, len(filedata))
     img0 = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR)
     cv.ShowImage("captchas", img0)
     cv.WaitKey()
     captchas = raw_input("Please enter the captchas:")
     cv.DestroyAllWindows()
     postdata = urllib.urlencode({"id": self.__id, "icode": captchas})
     req = urllib2.Request(url="http://www.renren.com/validateuser.do",
                           data=postdata)
     content = urllib2.urlopen(req).read()
Exemple #7
0
            dst, src2, (x, y), 40, cv.CV_INTER_LINEAR +
            cv.CV_WARP_FILL_OUTLIERS + cv.CV_WARP_INVERSE_MAP)
        cv.ShowImage("log-polar", dst)
        cv.ShowImage("inverse log-polar", src2)


if __name__ == "__main__":

    if len(sys.argv) > 1:
        src = cv.LoadImage(sys.argv[1], cv.CV_LOAD_IMAGE_COLOR)
    else:
        url = 'https://code.ros.org/svn/opencv/trunk/opencv/samples/c/fruits.jpg'
        filedata = urllib2.urlopen(url).read()
        imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1)
        cv.SetData(imagefiledata, filedata, len(filedata))
        src = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR)

    cv.NamedWindow("original", 1)
    cv.NamedWindow("log-polar", 1)
    cv.NamedWindow("inverse log-polar", 1)

    dst = cv.CreateImage((256, 256), 8, 3)
    src2 = cv.CreateImage(cv.GetSize(src), 8, 3)

    cv.SetMouseCallback("original", on_mouse)
    on_mouse(cv.CV_EVENT_LBUTTONDOWN, src.width / 2, src.height / 2, None,
             None)

    cv.ShowImage("original", src)
    cv.WaitKey()
Exemple #8
0
                cv.Ellipse(image04, center, size, angle, 0, 360, color, 2,
                           cv.CV_AA, 0)

        # Show image. HighGUI use.
        cv.ShowImage("Result", image04)


if __name__ == '__main__':
    if len(sys.argv) > 1:
        source_image = cv.LoadImage(sys.argv[1], cv.CV_LOAD_IMAGE_GRAYSCALE)
    else:
        url = 'https://raw.github.com/opencv/opencv/master/samples/c/stuff.jpg'
        filedata = urllib2.urlopen(url).read()
        imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1)
        cv.SetData(imagefiledata, filedata, len(filedata))
        source_image = cv.DecodeImage(imagefiledata,
                                      cv.CV_LOAD_IMAGE_GRAYSCALE)

    # Create windows.
    cv.NamedWindow("Source", 1)
    cv.NamedWindow("Result", 1)

    # Show the image.
    cv.ShowImage("Source", source_image)

    fe = FitEllipse(source_image, 70)

    print "Press any key to exit"
    cv.WaitKey(0)

    cv.DestroyWindow("Source")
    cv.DestroyWindow("Result")
Exemple #9
0
def url_jpg_contours():
    url = 'http://i12.tietuku.com/05ef0b29030fa46c.jpg'
    filedata = urllib2.urlopen(url).read()
    imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1)
    print imagefiledata  #<cvmat(type=42424000 8UC1 rows=1 cols=48230 step=48230 )>
    cv.SetData(imagefiledata, filedata, len(filedata))
    im = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR)
    col_edge = cv.CreateImage((im.width, im.height), 8, 3)

    # convert to grayscale
    gray_im = cv.CreateImage((im.width, im.height), 8, 1)
    edge_im = cv.CreateImage((im.width, im.height), 8, 1)
    cv.CvtColor(im, gray_im, cv.CV_BGR2GRAY)
    cv.Canny(gray_im, edge_im, position, position * 3, 3)
    cv.SetZero(col_edge)
    # copy edge points
    cv.Copy(im, col_edge, edge_im)
    #ret, edge_jpg = cv2.imencode('.jpg', edge_im, [int(cv.CV_IMWRITE_JPEG_QUALITY), 80])
    edge_im_array = np.asarray(edge_im[:])

    print type(edge_im_array)
    #edge_jpg_gray = cv2.cvtColor(edge_im_array,cv2.COLOR_BGR2GRAY)
    ret, edge_im_array = cv2.threshold(edge_im_array, 127, 255,
                                       cv2.THRESH_BINARY)
    print type(edge_im_array)
    contours, hierarchy = cv2.findContours(
        edge_im_array, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
    )  #压缩水平方向,垂直方向,对角线方向的元素,只保留该方向的终点坐标,例如一个矩形轮廓只需4个点来保存轮廓信息
    contours_img = cv2.cvtColor(edge_im_array, cv2.COLOR_GRAY2BGR)
    url_str_len_contours = str(len(contours))  #取轮廊数量
    str_len_contours = str(len(contours))  #取轮廊数量

    #数据处理

    first_contours = contours[0]  #第一条轨迹坐标集合,数据格式为numpy.ndarry

    first_contours_list = first_contours.tolist()
    #print contours                #输出所有轨迹坐标集合
    #print contours[-1]          #输出最后一条轨迹坐标,数据格式为numpy.ndarry
    #print contours[0][0].tolist()[0] #输出第一条轨迹起始点坐标[[375 241]]并转化成list格式[[375,241]] |**.tolist()[0] 可以省掉一个中括号输出[375,241]
    #print contours[0][0].tolist()[0][0] #输出第一条轨迹起始点坐标的X坐标值。
    #print contours[0][0].tolist()[0][1] #输出第一条轨迹起始点坐标的Y坐标值。

    #print [i[0][0] for i in contours]
    #print [i[0][0] for i in contours[0]]

    scale = 1  #不缩放
    contours_img = cv2.resize(contours_img, (0, 0), fx=scale, fy=scale)
    print "Url_jpg_contours_num:%s" % url_str_len_contours
    for cnt in contours:
        color = np.random.randint(0, 255, (3)).tolist()
        cv2.drawContours(contours_img, [cnt * scale], 0, color, 1)
    cv2.imshow("URL_canny_img", edge_im_array)
    cv2.imshow("URL_contours_img", contours_img)

    #轮廊清单转文本输出
    edge_im_array_pix = str(np.size(edge_im_array))
    contours_img_pix = str(np.size(contours_img))

    ss = open("Contours" + ".log", 'w')
    ss.write("edge_im_array_pix nums:" + "%s" % edge_im_array_pix + "\n")
    ss.write("contours_img_pix nums:" + "%s" % contours_img_pix + "\n")
    ss.write("_url_contours num:" + "%s" % str_len_contours + "\n")
    for ele in contours:
        ss.write("%s" % ele)
    ss.write("**" * 50 + "\n")
    ss.close()
    #return contours
    cv2.waitKey(0)