본문 바로가기

OpenCV

ch03 ScalarOp

#python code
import cv2
import numpy as np


def VecOp():
    p1 = np.zeros(3, dtype=np.uint8)  # Vec3b equivalent
    p2 = np.array([0, 0, 255], dtype=np.uint8)  # Vec3b equivalent

    p1[0] = 100
    print("p1:", p1)
    print("p2:", p2)


def ScalarOp():
    gray = np.array([128, 128, 128], dtype=np.uint8)  # Scalar equivalent
    print("gray:", gray)

    yellow = np.array([0, 255, 255], dtype=np.uint8)  # Scalar equivalent
    print("yellow:", yellow)

    img1 = np.full((256, 256, 3), yellow, dtype=np.uint8)

    for i in range(4):
        if i < len(yellow):
            print(yellow[i], end=", ")
        else:
            print("0", end=", ")
        if i < len(yellow):
            print(yellow[i])
        else:
            print("0")


if __name__ == "__main__":
    VecOp()
    ScalarOp()

// C++ code 
#include "opencv2/opencv.hpp"
#include <iostream>

using namespace cv;
using namespace std;

void VecOp();
void ScalarOp();

int main(void)
{
	VecOp();
	ScalarOp();

	return 0;
}

void VecOp()
{
	Vec3b p1, p2(0, 0, 255);
	p1[0] = 100;
	cout << "p1: " << p1 << endl;
	cout << "p2: " << p2 << endl;
}

void ScalarOp()
{
	Scalar gray = 128;
	cout << "gray: " << gray << endl;

	Scalar yellow(0, 255, 255);
	cout << "yellow: " << yellow << endl;

	Mat img1(256, 256, CV_8UC3, yellow);

	for (int i = 0; i < 4; i++)
		cout << yellow.val[i] << ", " << yellow[i] << endl;
}

C++ Code 출처 : OpenCV 4로 배우는 컴퓨터 비전과 머신 러닝 - 황선규 저


p1: [100   0   0]
p2: [  0   0 255]
gray: [128 128 128]
yellow: [  0 255 255]
0, 0
255, 255
255, 255
0, 0

'OpenCV' 카테고리의 다른 글

ch04 keyboard  (0) 2024.05.19
ch04 drawing  (0) 2024.05.19
ch03 MatOp  (0) 2024.05.19
ch03 InputArrayOp  (0) 2024.05.19
ch03 BasicOp  (0) 2024.05.19