总是混淆的HW

python

import cv2
import numpy as np

'''
opencv的image数据格式就是用numpy unit8 格式存储的。两者之间可以相互转换
'''

img = cv2.imread("../img/1.jpg")
print(img.shape)  # (1080, 1920, 3) HWC
print(img.dtype)  # dtype('uint8')

img = cv2.resize(img, (640, 320), dytpe=np.uint8)  # resize参数:W,H
# cv2.imshow("cc", img)
# cv2.waitKey()
print(img.shape)  # (320, 640, 3) HWC

new_img = np.zeros((1080, 1920, 3), dtype=np.uint8)  # HWC
cv2.imshow("cc", new_img) #如果不是uint8格式,那么imshow是会自动转换
cv2.waitKey()
print(new_img.shape)

 

c++

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;


int main() {
    Mat img = imread("zidane.jpg"); //图片是1280*720
    cout << sizeof(img) << endl;
    int rows = img.rows; //720   矩阵的行数  高
    int cols = img.cols; //1280  矩阵的列数  宽
    int channels = img.channels();
    cout << "rows:" << rows << " cols:" << cols << "channels:"<<channels<< endl;

    cout << "size:" << img.size() << endl; //size:[1280 x 720]
    cout << "width:" << img.size().width << endl; //width:1280
    cout << "height:" << img.size().height << endl; //height:720

    Size dsize = Size(800, 600);//宽 高
    resize(img, img, dsize, 0, 0, cv::INTER_AREA);
    cout << "size:" << img.size() << endl; //size:[800 x 600]


    // 构建Mat
    Mat a1(1280, 640, CV_8UC3);
    int rows1 = a1.rows; //1280   矩阵的行数  高
    int cols1 = a1.cols; //640  矩阵的列数  宽
    int channels1 = a1.channels();
    cout << "rows1:" << rows1 << " cols1:" << cols1 << "channels1:" << channels1 << endl;


    //Rect初始化
    cv::Point point1 = cv::Point(10, 10);
    cv::Point point2 = cv::Point(20,20);
    cv::Rect boxPoint = cv::Rect(point1, point2);//可以通过对角点来初始化,但Rect只有x,y,width,heigth的属性
    
    cv::Rect box = cv::Rect(10,20,100,300); //通过x,y,width,heigth来初始化
    return 0;
}

 

posted @ 2024-01-23 09:16  cheng4632  阅读(5)  评论(0编辑  收藏  举报