2019-06-23(day009)——python中的新知识点 - cv.COLOR_BGR2YUV - cv.COLOR_BGR2HSV - cv.COLOR_BGR2CrCb - cv.i...

python

# -*- coding:utf-8 -*-
import cv2 as cv

def function():
    img = cv.imread("../images/cat.jpg")
    cv.imshow("cat_bgr", img)
    #与直方图有关的图像处理转化到HSV比较好
    img_hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
    cv.imshow("cat_hsv", img_hsv)
    #和设备有关,Android相机拍出来的照片为yuv格式
    img_yuv = cv.cvtColor(img, cv.COLOR_BGR2YUV)
    cv.imshow("cat_yuv", img_yuv)
    #做皮肤检测,根据一些颜色的区分比较好
    img_ycrcb = cv.cvtColor(img, cv.COLOR_BGR2YCrCb)
    cv.imshow("cat_ycrcb", img_ycrcb)
    
    #下面演示cv.inRange函数,也可以看出HSV的一个应用
    img1 = cv.imread("../images/aloeL.jpg")
    cv.imshow("tree", img1)
    hsv = cv.cvtColor(img1, cv.COLOR_BGR2HSV)
    mask = cv.inRange(hsv, (35, 43, 46), (77, 255, 255))
    cv.imshow("mask", mask)
    
function()
cv.waitKey(0)
cv.destroyAllWindows()

python中的新知识点

  • cv.COLOR_BGR2YUV
  • cv.COLOR_BGR2HSV
  • cv.COLOR_BGR2CrCb
  • cv.inRange()

c++

#include "all.h"
using namespace std;
using namespace cv;

void MyClass::day009() {
    Mat img = read(PATH + "\\images\\cat.jpg");

    Mat dst1, dst2, dst3, mask, hsv;
    cvtColor(img, dst1, COLOR_BGR2HSV);
    imshow("HSV", dst1);

    cvtColor(img, dst2, COLOR_BGR2YCrCb);
    imshow("CrCb", dst2);

    cvtColor(img, dst3, COLOR_BGR2YUV);
    imshow("YUV", dst3);

    Mat img1 = read(PATH + "\\images\\tree.jpg");
    cvtColor(img, hsv, COLOR_BGR2HSV);
    inRange(hsv, Scalar(35, 43, 46), Scalar(99, 255, 255), mask);
    imshow("mask", mask);
    waitKey(0);
}

c++中新知识点

  • COLOR_BGR2HSV
  • COLOR_BGR2YCrCb
  • COLOR_BGR2YUV
  • inRange()

此处注意inRange函数中的参数,在Python和c++中的区别

推荐阅读更多精彩内容