zoukankan      html  css  js  c++  java
  • 吴裕雄 实战PYTHON编程(9)

    import cv2

    cv2.namedWindow("ShowImage1")
    cv2.namedWindow("ShowImage2")
    image1 = cv2.imread("F:\pythonBasepythonex\ch10\media\img01.jpg")
    #image1 = cv2.imread("media\img01.jpg", 1)
    image2 = cv2.imread("F:\pythonBasepythonex\ch10\media\img01.jpg", 0)
    cv2.imshow("ShowImage1", image1)
    cv2.imshow("ShowImage2", image2)
    # cv2.waitKey()
    cv2.waitKey(10000)
    cv2.destroyAllWindows()

    import cv2

    cv2.namedWindow("ShowImage")
    image = cv2.imread("F:\pythonBasepythonex\ch10\media\img01.jpg", 0)
    cv2.imshow("ShowImage", image)
    cv2.imwrite("F:\pythonBasepythonex\ch10\img01copy1.jpg", image)
    cv2.imwrite("F:\pythonBasepythonex\ch10\img01copy2.jpg", image, [int(cv2.IMWRITE_JPEG_QUALITY), 50])
    cv2.waitKey(10000)
    cv2.destroyWindow("ShowImage")

    import cv2, numpy

    cv2.namedWindow("plot")
    image = cv2.imread("F:\pythonBasepythonex\ch10\media\background.jpg")
    cv2.line(image, (50,50), (300,300), (255,0,0), 2)
    cv2.rectangle(image, (500,20), (580,100), (0,255,0), 3)
    cv2.rectangle(image, (100,300), (150,360), (0,0,255), -1)
    cv2.circle(image, (500,300), 40, (255,255,0), -1)
    pts = numpy.array([[300,300],[300,340],[350,320]], numpy.int32)
    cv2.polylines(image, [pts], True, (0,255,255), 2)
    cv2.putText(image,"background.jpg", (350,420), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
    cv2.imshow("plot", image)
    cv2.waitKey(5000)
    cv2.destroyAllWindows()

    import cv2

    casc_path = "C:\Users\acer\Anaconda3\envs\python36\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml"
    faceCascade = cv2.CascadeClassifier(casc_path)
    imagename = cv2.imread("F:\pythonBasepythonex\ch10\media\person1.jpg")
    faces = faceCascade.detectMultiScale(imagename, scaleFactor=1.1, minNeighbors=5, minSize=(30,30), flags = cv2.CASCADE_SCALE_IMAGE)
    #imagename.shape[0]:图片高度,imagename.shape[1]:图片宽度
    cv2.rectangle(imagename, (10,imagename.shape[0]-20), (110,imagename.shape[0]), (0,0,0), -1)
    cv2.putText(imagename,"Find " + str(len(faces)) + " face!", (10,imagename.shape[0]-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2)
    for (x,y,w,h) in faces:
    cv2.rectangle(imagename,(x,y),(x+w, y+h),(128,255,0),2)
    cv2.namedWindow("facedetect")
    cv2.imshow("facedetect", imagename)
    cv2.waitKey(0)
    cv2.destroyWindow("facedetect")

    import cv2

    # casc_path = "C:\ProgramData\Anaconda3\pkgs\opencv3-3.1.0-py27_0\Libraryetc\haarcascades\haarcascade_frontalface_default.xml"
    casc_path = "C:\Users\acer\Anaconda3\envs\python36\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml"
    faceCascade = cv2.CascadeClassifier(casc_path)
    image = cv2.imread("F:\pythonBasepythonex\ch10\media\person3.jpg")
    faces = faceCascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5, minSize=(30,30), flags = cv2.CASCADE_SCALE_IMAGE)
    #image.shape[0]:取得图片高度,image.shape[1]:取得图片宽度
    cv2.rectangle(image, (10,image.shape[0]-20), (110,image.shape[0]), (0,0,0), -1)
    cv2.putText(image,"Find " + str(len(faces)) + " face!", (10,image.shape[0]-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2)
    for (x,y,w,h) in faces:
    cv2.rectangle(image,(x,y),(x+w, y+h),(128,255,0),2)
    cv2.namedWindow("facedetect")
    cv2.imshow("facedetect", image)
    cv2.waitKey(10000)
    cv2.destroyWindow("facedetect")

    import cv2

    # casc_path = "C:\ProgramData\Anaconda3\pkgs\opencv3-3.1.0-py27_0\Libraryetc\haarcascades\haarcascade_frontalface_default.xml"
    casc_path = "C:\Users\acer\Anaconda3\envs\python36\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml"
    faceCascade = cv2.CascadeClassifier(casc_path)
    image = cv2.imread("F:\pythonBasepythonex\ch10\media\person8.jpg")
    faces = faceCascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5, minSize=(30,30), flags = cv2.CASCADE_SCALE_IMAGE)
    #image.shape[0]:获取图片高度,image.shape[1]:获取图片宽度
    cv2.rectangle(image, (10,image.shape[0]-20), (110,image.shape[0]), (0,0,0), -1)
    cv2.putText(image,"Find " + str(len(faces)) + " face!", (10,image.shape[0]-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2)
    for (x,y,w,h) in faces:
    cv2.rectangle(image,(x,y),(x+w, y+h),(128,255,0),2)
    cv2.namedWindow("facedetect")
    cv2.imshow("facedetect", image)
    cv2.waitKey(10000)
    cv2.destroyWindow("facedetect")

    import cv2

    from PIL import Image

    # casc_path = "C:\ProgramData\Anaconda3\pkgs\opencv3-3.1.0-py27_0\Libraryetc\haarcascades\haarcascade_frontalface_default.xml"
    casc_path = "C:\Users\acer\Anaconda3\envs\python36\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml"
    faceCascade = cv2.CascadeClassifier(casc_path)
    imagename = "F:\pythonBasepythonex\ch10\media\person1.jpg"
    image = cv2.imread(imagename)
    faces = faceCascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5, minSize=(30,30), flags = cv2.CASCADE_SCALE_IMAGE)
    count = 1
    for (x,y,w,h) in faces:
    cv2.rectangle(image, (x,y), (x+w,y+h), (128,255,0), 2)
    filename = "F:\pythonBasepythonex\ch10\" + str(count)+ ".jpg"
    image1 = Image.open(imagename)
    image2 = image1.crop((x, y, x+w, y+h))
    image3 = image2.resize((200, 200), Image.ANTIALIAS)
    image3.save(filename)
    count += 1
    cv2.namedWindow("facedetect")
    cv2.imshow("facedetect", image)
    cv2.waitKey(10000)
    cv2.destroyWindow("facedetect")

    import cv2
    from PIL import Image

    # casc_path = "C:\ProgramData\Anaconda3\pkgs\opencv3-3.1.0-py27_0\Libraryetc\haarcascades\haarcascade_frontalface_default.xml"
    casc_path = "C:\Users\acer\Anaconda3\envs\python36\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml"
    faceCascade = cv2.CascadeClassifier(casc_path)
    imagename = "F:\pythonBasepythonex\ch10\media\person3.jpg"
    image = cv2.imread(imagename)
    faces = faceCascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5, minSize=(30,30), flags = cv2.CASCADE_SCALE_IMAGE)
    count = 1
    for (x,y,w,h) in faces:
    cv2.rectangle(image, (x,y), (x+w,y+h), (128,255,0), 2)
    filename = "F:\pythonBasepythonex\" + str(count)+ ".jpg"
    image1 = Image.open(imagename)
    image2 = image1.crop((x, y, x+w, y+h))
    image3 = image2.resize((200, 200), Image.ANTIALIAS)
    image3.save(filename)
    count += 1
    cv2.namedWindow("facedetect")
    cv2.imshow("facedetect", image)
    cv2.waitKey(10000)
    cv2.destroyWindow("facedetect")

    import cv2
    from PIL import Image

    # casc_path = "C:\ProgramData\Anaconda3\pkgs\opencv3-3.1.0-py27_0\Libraryetc\haarcascades\haarcascade_frontalface_default.xml"
    casc_path = "C:\Users\acer\Anaconda3\envs\python36\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml"
    faceCascade = cv2.CascadeClassifier(casc_path)
    imagename = "F:\pythonBasepythonex\ch10\media\person8.jpg"
    image = cv2.imread(imagename)
    faces = faceCascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5, minSize=(30,30), flags = cv2.CASCADE_SCALE_IMAGE)
    count = 1
    for (x,y,w,h) in faces:
    cv2.rectangle(image, (x,y), (x+w,y+h), (128,255,0), 2)
    filename = "F:\pythonBase\pythonex\" + str(count)+ ".jpg"
    image1 = Image.open(imagename)
    image2 = image1.crop((x, y, x+w, y+h))
    image3 = image2.resize((200, 200), Image.ANTIALIAS)
    image3.save(filename)
    count += 1
    cv2.namedWindow("facedetect")
    cv2.imshow("facedetect", image)
    cv2.waitKey(10000)
    cv2.destroyWindow("facedetect")

  • 相关阅读:
    POJ 1222 POJ 1830 POJ 1681 POJ 1753 POJ 3185 高斯消元求解一类开关问题
    POJ 3237 Tree (树链剖分)
    2038: [2009国家集训队]小Z的袜子(hose) (莫队算法)
    HDU 4685 Prince and Princess (2013多校8 1010题 二分匹配+强连通)
    HDU 4678 Mine (2013多校8 1003题 博弈)
    HDU 4679 Terrorist’s destroy (2013多校8 1004题 树形DP)
    HDU 4681 String(2013多校8 1006题 DP)
    1036: [ZJOI2008]树的统计Count (树链剖分)
    HDU 3966 Aragorn's Story (树链剖分+树状数组)
    PHP服务端支付宝支付及回调
  • 原文地址:https://www.cnblogs.com/tszr/p/10061382.html
Copyright © 2011-2022 走看看