0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

OpenCV自分用メモ

Last updated at Posted at 2021-12-01

メモです.

動画ファイル読込み

video.open("D:\\Download\\cart1.mp4");
if (video.isOpened() == false) {
  return 0;
}
video >> currentFrame;  //1フレームずつ取り出し

動画ファイル書き出し

 そのファイル名でファイルが存在する場合は,最後尾に追加する

int width = video.get(cv::CAP_PROP_FRAME_WIDTH);
int height = video.get(cv::CAP_PROP_FRAME_HEIGHT);
int fps = video.get(cv::CAP_PROP_FPS);
int fourcc = cv::VideoWriter::fourcc('M', 'P', '4', 'V');
cv::VideoWriter output("D:\\Download\\cart1a.mp4", fourcc, fps, cv::Size(width, height),0);
output << calcFrame;  //1フレーム書き込み

グレースケール化

cv::cvtColor(currentFrame, calcFrame, cv::COLOR_BGR2GRAY);

GaussianBlur(ガウシアンブラー)

cv::GaussianBlur(currentFrame, currentFrame, cv::Size(3,3), 0);

Canny Edge(キャニーエッジ)

cv::Canny(currentFrame, currentFrame, cannyValue1, 200);

absolute difference(2枚の画像の差分)

cv::absdiff(baseFrame, currentFrame, calcFrame);

erode

cv::erode(calcFrame, calcFrame, cv::Mat(), cv::Point(-1, -1), 1); //カーネル3x3
cv::erode(calcFrame, calcFrame, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5)), cv::Point(-1, -1), 1); //カーネル5x5

2値化

cv::cvtColor(currentFrame, currentFrame, cv::COLOR_BGR2GRAY);  //まずグレースケールにする
cv::threshold(inputFrame, outputFrame, value, 255, cv::THRESH_BINARY);  //value=閾値

morphologyEx(クロージング)

cv::morphologyEx(currentFrame, currentFrame, cv::MORPH_CLOSE, cv::Mat(), cv::Point(-1, -1), 1);

BackGroundSubtractor

cv::Ptr<cv::BackgroundSubtractor> bgsubtractor = cv::createBackgroundSubtractorMOG2();
bgsubtractor->apply(calcFrame, calcFrame, 0);  //背景更新しない
bgsubtractor->apply(currentFrame, calcFrame, 1);  //最終フレームで背景を更新
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?