Last active
September 20, 2017 08:41
-
-
Save HaruhiroTakahashi/f2c10b72e60c529e78c9d4fb3d9369b0 to your computer and use it in GitHub Desktop.
openCVを使ってみる(クラスにまとめてみる編)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include "OCV.h" | |
| #include <random> | |
| using namespace::std; | |
| using namespace::cv; | |
| using cv::UMat; | |
| IAffine::~IAffine() | |
| { | |
| cout << "IAffine破棄されたよ" << endl; | |
| } | |
| UMat OCV::Frip() | |
| { | |
| int flipCode = atoi("-1"); //atoi関数 = 文字列を数値に変換してくれるらしい 興味本位で使用 | |
| flip(srcImg, dstImg, flipCode); | |
| return dstImg; | |
| } | |
| UMat OCV::Scaling() | |
| { | |
| float scaleW = static_cast<float>(atof("1.5")); | |
| float scaleH = static_cast<float>(atof("0.5")); | |
| int width = static_cast<int>(srcImg.cols*scaleW); | |
| int hight = static_cast<int>(srcImg.rows*scaleH); | |
| resize(srcImg, dstImg, Size(width, hight)); | |
| return dstImg; | |
| } | |
| UMat OCV::Rotate() | |
| { | |
| float angle = static_cast<float>(atof("23.2")); | |
| Point2f center = Point2f(static_cast<float>(srcImg.cols / 2), static_cast<float>(srcImg.rows / 2)); | |
| getRotationMatrix2D(center, angle, 1.0).copyTo(affintrance); | |
| warpAffine(srcImg, dstImg, affintrance, dstImg.size(), INTER_CUBIC, BORDER_REPLICATE); | |
| return dstImg; | |
| } | |
| UMat OCV::RotateCont() | |
| { | |
| Point2f center = Point2f(static_cast<float>(srcImg.cols / 2), static_cast<float>(srcImg.rows / 2)); | |
| namedWindow("test", CV_WINDOW_AUTOSIZE); | |
| for (float angle = 0.0; angle < 360.0; angle += 90.0) | |
| { | |
| Mat affintrance = getRotationMatrix2D(center, angle, 1.0); | |
| warpAffine(srcImg, dstImg, affintrance, srcImg.size(), INTER_CUBIC); | |
| imshow("test", dstImg); | |
| if (cvWaitKey(1000) >= 0) { break; } | |
| //参考:https ://stackoverflow.com/questions/41750171/opencv-waitkey-function-always-returns-255-on-mac | |
| } | |
| return dstImg; | |
| } | |
| UMat OCV::Perspective() | |
| { | |
| Point2f dstPoint[4]; | |
| int xMergin, yMergin; | |
| int x0 = srcImg.cols / 4; | |
| int x1 = (srcImg.cols / 4) * 3; | |
| int y0 = srcImg.rows / 4; | |
| int y1 = (srcImg.rows / 4) * 3; | |
| Point2f srcPoint[] = { | |
| Point(x0, y0), | |
| Point(x0, y1), | |
| Point(x1, y1), | |
| Point(x1, y0) | |
| }; | |
| //乱数を使いたいときC++では<random>ヘッダを使用する。 | |
| //以下のように書いてdist(mt)を呼ぶと0~2がランダムで出力される。 | |
| //正直よくわからないので、参考サイトを要勉強(http://qiita.com/_EnumHack/items/25fc998873d1bc7d2276) | |
| mt19937 mt{ random_device{}() }; | |
| uniform_int_distribution<int> dist(0, 2); | |
| switch (dist(mt)) | |
| { | |
| //前に倒した様な透視変換 | |
| case 0: | |
| xMergin = srcImg.cols / 10; | |
| yMergin = srcImg.rows / 10; | |
| dstPoint[0] = Point(x0 + xMergin, y0 + yMergin); | |
| dstPoint[1] = srcPoint[1]; | |
| dstPoint[2] = srcPoint[2]; | |
| dstPoint[3] = Point(x1 - xMergin, y0 + yMergin); | |
| break; | |
| //扉を開けた様な透視変換 | |
| case 1: | |
| xMergin = srcImg.cols / 8; | |
| yMergin = srcImg.rows / 8; | |
| dstPoint[0] = srcPoint[0]; | |
| dstPoint[1] = srcPoint[1]; | |
| dstPoint[2] = Point(x1 - xMergin, y1 - yMergin); | |
| dstPoint[3] = Point(x1 - xMergin, y0 + yMergin); | |
| break; | |
| //斜めに引き延ばす透視変換 | |
| case 2: | |
| xMergin = srcImg.cols / 6; | |
| yMergin = srcImg.rows / 6; | |
| dstPoint[0] = Point(x0 + xMergin, y0 + yMergin); | |
| dstPoint[1] = srcPoint[1]; | |
| dstPoint[2] = Point(x1 - xMergin, y1 - yMergin); | |
| dstPoint[3] = srcPoint[3]; | |
| break; | |
| default: | |
| cerr << "設定されていない値が入力されました。" << endl; | |
| break; | |
| } | |
| dstImg = NULL; //出力するdstImgは空じゃないとだめらしい | |
| Mat perspectiveMmat = getPerspectiveTransform(srcPoint, dstPoint); | |
| warpPerspective(srcImg, dstImg, perspectiveMmat, srcImg.size(), INTER_CUBIC); | |
| return dstImg; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #pragma warning(disable:4819) //Unicode文字に変えろというエラーを黙らせる | |
| #include <opencv2/opencv.hpp> | |
| #pragma warning(default:4819) | |
| #define DEBUG | |
| #ifdef DEBUG | |
| #pragma comment(lib, "opencv_world320d.lib") | |
| #else | |
| #pragma comment(lib, "opencv_world320.lib") | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include "OCV.h" | |
| using namespace::std; | |
| using namespace::cv; | |
| using cv::UMat; | |
| IGraphics::~IGraphics() | |
| { | |
| cout << "IGraphics破棄されたよ" << endl; | |
| } | |
| UMat OCV::drawCircle() | |
| { | |
| dstImg = srcImg; | |
| circle(dstImg, Point(200, 200), 50, Scalar(255, 0, 0), 2); | |
| //circle(imgs[1], Point(200, 200), 100, Scalar(0, 255, 0), 20); | |
| //circle(imgs[2], Point(200, 200), 150, Scalar(0, 0, 255), -1); | |
| //ostringstream oss; | |
| //oss << "CIRCLE" << i; | |
| //string imgJpg = oss.str(); | |
| //imshow(imgJpg, imgs[i]); | |
| return dstImg; | |
| } | |
| UMat OCV::drawLine() | |
| { | |
| dstImg = srcImg; | |
| int x0 = srcImg.cols / 4; | |
| int x1 = srcImg.cols * 3 / 4; | |
| int y0 = srcImg.rows / 4; | |
| int y1 = srcImg.rows * 3 / 4; | |
| Point p0 = Point(x0, y0); | |
| Point p1 = Point(x1, y1); | |
| dstImg = srcImg; | |
| line(dstImg, p0, p1, Scalar(0, 0, 255), 3, 4); | |
| p0.y = y1; | |
| p1.y = y0; | |
| line(dstImg, p0, p1, Scalar(255, 0, 0), 3, 4); | |
| return dstImg; | |
| } | |
| UMat OCV::drawEcllipse() | |
| { | |
| dstImg = srcImg; | |
| Point center = Point(srcImg.cols / 2, srcImg.rows / 2); | |
| Size sz = Size(srcImg.cols / 2, srcImg.rows / 2); | |
| ellipse(dstImg, center, sz, 0, 0, 360, Scalar(255, 0, 0), 3, 4); | |
| sz.width -= 20; | |
| sz.height -= 50; | |
| ellipse(dstImg, center, sz, 15, 10, 360, Scalar(255, 255, 0), 2, 4); | |
| return dstImg; | |
| } | |
| UMat OCV::drawRect() | |
| { | |
| dstImg = srcImg; | |
| Point p0 = Point(srcImg.cols / 8, srcImg.rows / 8); | |
| Point p1 = Point(srcImg.cols * 7 / 8, srcImg.rows * 7 / 8); | |
| rectangle(dstImg, p0, p1, Scalar(0, 255, 0), 5, 8); | |
| Point p2 = Point(srcImg.cols * 2 / 8, srcImg.rows * 2 / 8); | |
| Point p3 = Point(srcImg.cols * 6 / 8, srcImg.rows * 6 / 8); | |
| rectangle(dstImg, p2, p3, Scalar(0, 255, 255), 2, 4); | |
| return dstImg; | |
| } | |
| UMat OCV::drawText() | |
| { | |
| dstImg = srcImg; | |
| Point p = Point(50, srcImg.rows / 2 - 50); | |
| // 画像,文字,開始位置,フォント,大きさ,色,線幅,種類 | |
| putText(dstImg, "Hello OpenCV", p, FONT_HERSHEY_TRIPLEX, 0.8, Scalar(250, 200, 200), 2, CV_AA); | |
| return dstImg; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include "pch.h" | |
| using cv::UMat; | |
| class IAffine | |
| { | |
| public: | |
| virtual ~IAffine(); | |
| public: | |
| virtual UMat Frip() = 0; | |
| virtual UMat Scaling() = 0; | |
| virtual UMat Rotate() = 0; | |
| virtual UMat RotateCont() = 0; | |
| virtual UMat Perspective() = 0; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include "pch.h" | |
| using cv::UMat; | |
| class IGraphics | |
| { | |
| public: | |
| virtual ~IGraphics(); | |
| public: | |
| virtual UMat drawCircle() = 0; | |
| virtual UMat drawLine() = 0; | |
| virtual UMat drawEcllipse() = 0; | |
| virtual UMat drawRect() = 0; | |
| virtual UMat drawText() = 0; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include "OCV.h" | |
| using namespace::std; | |
| using namespace::cv; | |
| void main(void) | |
| { | |
| char szDirectoryName[MAX_PATH]; | |
| GetCurrentDirectory(sizeof(szDirectoryName), szDirectoryName); | |
| //static_cast<string>(szDirectoryName)でもキャストできる | |
| //たぶん暗黙的か明示的かの違い | |
| //下記のサイトの下の方に解説が載っている | |
| //https://social.msdn.microsoft.com/Forums/ja-JP/5ee435a0-970a-40ef-85ad-1895e938c27d/stdstring?forum=vcgeneralja | |
| const string path = (string)szDirectoryName + "\\D69N新諸元リア販社マーカー.bmp"; | |
| //const string path = (string)szDirectoryName + "\\Lenna.jpg"; | |
| cout << "Current Directory : " << szDirectoryName << endl; | |
| try | |
| { | |
| UMat srcImg, dstImg; | |
| imread(path).copyTo(srcImg); | |
| if (srcImg.empty()) { throw ("開けなかったよ。");} | |
| imshow("Befor", srcImg); | |
| IAffine *img = new OCV(srcImg); | |
| dstImg = img->Frip(); | |
| if (dstImg.empty() == false) | |
| imshow("After", dstImg); | |
| delete img; | |
| img = nullptr; | |
| //imwrite(path, aImg); | |
| waitKey(0); | |
| destroyAllWindows(); | |
| } | |
| catch (const char* str) | |
| { | |
| cerr << str << endl; | |
| getchar(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include "pch.h" | |
| //参考:http://bituse.info/c/33 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| //以下に共通して読み込みたいヘッダをincludeする | |
| #include "common.h" | |
| #include <string> | |
| #include <iostream> | |
| #include <Windows.h> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment