model52.py-20170918

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 18 17:16:49 2017

@author: vicky
"""

import cv2
import numpy as np

#class Findposition:
#    def __init__(self,path):
#    #获取图片
#        self.img=cv2.imread(path)
#        self.gray=cv2.cvtColor(self.img,cv2.COLOR_BGR2GRAY)
#        self.hsv=cv2.cvtColor(self.img,cv2.COLOR_BGR2HSV)
#
#    #提取黑色的区域 
#    def Get_black(self):
#        #get black area
#        low_black=np.array([0,0,0])
#        high_black=np.array([100,100,100])
#        mask=cv2.inRange(self.img,low_black,high_black)
#        black=cv2.bitwise_and(self.hsv,self.hsv,mask=mask)
#        return black
#
#    #将黑色区域进行二值化处理 
#    def Get_contour(self):
#        #change to gray
#        black=self.Get_black()
#        black_gray=cv2.cvtColor(black,cv2.COLOR_HSV2BGR)
#        black_gray=cv2.cvtColor(black_gray,cv2.COLOR_BGR2GRAY)
#        
#        #binaryzation
#        _, thresh=cv2.threshold(black_gray,10,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
#        img_morph=cv2.morphologyEx(thresh,cv2.MORPH_OPEN,(3,3))
#        cv2.erode(img_morph,(3,3),img_morph,iterations=2)
#        cv2.dilate(img_morph,(3,3),img_morph,iterations=2)
#        return img_morph
#    
#    #获取中心区域轮廓及坐标 
#    def Find_contour(self,img):
#        img_cp=img.copy()
#        (_,cnts,_)=cv2.findContours(img_cp,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
#        cnt_second=sorted(cnts,key=cv2.contourArea,reverse=True)[1]
#        box =cv2.minAreaRect(cnt_second)
#        return np.int0(cv2.boxPoints(box))
#    
#    #绘制轮廓
#    def Draw_contour(self,points):
#        mask=np.zeros(self.gray.shape,np.uint8)
#        cv2.drawContours(mask,[points],-1,255,2)
#        return mask
#
#    #获取中心位置
#    def Get_center(self,points):
#        p1x,p1y=points[0,0],points[0,1]
#        p3x,p3y=points[2,0],points[2,1]
#        center_x,center_y=(p1x+p3x)/2,(p1y+p3y)/2
#        center=(int(center_x),int(center_y))
#        return center
#
#    #绘制中心点
#    def Draw_center(self,center,mask):
#        cv2.circle(mask,center,1,(255,255,255),2)
#        return mask
#
#    #主函数
#    def main_process(self):
#        morph=self.Get_contour()
#        black=self.Get_black()
#        points=self.Find_contour(morph)
#        mask=self.Draw_contour(points)
#        center=self.Get_center(points)
#        draw_center=self.Draw_center(center,mask)
#        center_x,center_y=self.Get_center(points)
#
#        print(center_x,center_y)
#        cv2.imshow('black',black)
#        cv2.imshow('morph',morph)
#        cv2.imshow('img',self.img)
#        cv2.imshow('contour',draw_center)
#        cv2.waitKey(0)
#       
#
#if __name__== '__main__' :
#    path='/Users/vicky/Desktop/2.png'
#    d = Findposition(path)
#    d.main_process()

import the necessary packages
import numpy as np
import cv2
 
def find_marker(image):
    # convert the image to grayscale, blur it, and detect edges
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = cv2.Canny(gray, 35, 125)
 
    # find the contours in the edged image and keep the largest one;
    # we'll assume that this is our piece of paper in the image
    (_,cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    c = max(cnts, key = cv2.contourArea)
 
    # compute the bounding box of the of the paper region and return it
    return cv2.minAreaRect(c)
 
def distance_to_camera(knownWidth, focalLength, perWidth):
    # compute and return the distance from the maker to the camera
    return (knownWidth * focalLength) / perWidth
 
# initialize the known distance from the camera to the object, which
# in this case is 24 inches
KNOWN_DISTANCE = 24.0
 
# initialize the known object width, which in this case, the piece of
# paper is 11 inches wide
KNOWN_WIDTH = 11.0
 
# initialize the list of images that we'll be using
IMAGE_PATHS = ['/Users/vicky/Desktop/2.jpg', '/Users/vicky/Desktop/3.jpg', '/Users/vicky/Desktop/4.jpg']
 
# load the furst image that contains an object that is KNOWN TO BE 2 feet
# from our camera, then find the paper marker in the image, and initialize
# the focal length
image = cv2.imread(IMAGE_PATHS[0])
marker = find_marker(image)
focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH


def find_marker(image):
    # convert the image to grayscale, blur it, and detect edges
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (5, 5), 0)
    edged = cv2.Canny(gray, 35, 125)
 
    # find the contours in the edged image and keep the largest one;
    # we'll assume that this is our piece of paper in the image
    (cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    c = max(cnts, key = cv2.contourArea)
 
    # compute the bounding box of the of the paper region and return it
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值