Check out official documentation: OpenCV documentation
More tutorials: OpenCV tutorials
Dataset for 2nd Lecture: Dataset Download
cv::Mat image = cv::imread("C:\\Users\\Matej\\Pictures\\bird.jpg");
cv::imshow("image", image);
cv::waitKey();
cv::Mat grayscale;
cv::cvtColor(image, grayscale, cv::COLOR_BGR2GRAY);
cv::imshow("grayscale", grayscale);
cv::waitKey();
cv::Mat thresholded;
cv::threshold(grayscale, thresholded, 120, 255, cv::THRESH_BINARY);
cv::imshow("thresholded", thresholded);
cv::waitKey();
cv::Mat adaptiveThresholded;
cv::adaptiveThreshold(grayscale, adaptiveThresholded, 255,
cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 11, 0);
cv::imshow("adaptiveThresholded", adaptiveThresholded);
cv::waitKey();
cv::Mat canny;
cv::Canny(grayscale, canny, 10, 30);
cv::imshow("canny", canny);
cv::waitKey();
cv::Mat canvas = cv::Mat(215, 279, CV_8UC3, cv::Scalar(0, 0, 0));
cv::rectangle(canvas, cv::Rect(50, 100, 100, 100),
cv::Scalar(0, 0, 255));
cv::line(canvas, cv::Point(50, 100), cv::Point(100, 25),
cv::Scalar(0, 255, 0));
cv::line(canvas, cv::Point(150, 100), cv::Point(100, 25),
cv::Scalar(0, 255, 0));
cv::circle(canvas, cv::Point(175, 20), 20,
cv::Scalar(0, 255, 255), -1);
cv::imshow("canvas", canvas);
cv::waitKey();
cv::VideoCapture cap(0);
if (cap.isOpened())
{
cv::Mat frame;
while (1)
{
cap >> frame;
cv::imshow("Camera", frame);
if (cv::waitKey(30) >= 0) break;
}
}
Note sub matrix selection with cv::Rect
cv::Mat result(3 * image.rows, image.cols, image.type());
cv::blur(image, cv::Mat(result, cv::Rect(0, 0, image.cols, image.rows)), cv::Size(7, 7));
cv::GaussianBlur(image, cv::Mat(result, cv::Rect(0, image.rows, image.cols, image.rows)), cv::Size(7, 7), 0);
cv::medianBlur(image, cv::Mat(result, cv::Rect(0, 2*image.rows, image.cols, image.rows)), 7);
cv::putText(result, "cv::blur", cv::Point(0, 30), cv::FONT_HERSHEY_PLAIN, 1.0, cv::Scalar(0, 255, 0));
cv::putText(result, "cv::GaussianBlur", cv::Point(0, image.rows + 30), cv::FONT_HERSHEY_PLAIN, 1.0, cv::Scalar(0, 255, 0));
cv::putText(result, "cv::medianBlur", cv::Point(0, 2*image.rows + 30), cv::FONT_HERSHEY_PLAIN, 1.0, cv::Scalar(0, 255, 0));
cv::imshow("result", result);
cv::waitKey();
cv::Mat grayscale, thresholded, eroded;
cv::cvtColor(image, grayscale, cv::COLOR_BGR2GRAY);
cv::threshold(grayscale, thresholded, 120, 255, cv::THRESH_BINARY);
cv::erode(thresholded, eroded, cv::Mat());
cv::imshow("thresholded", thresholded);
cv::imshow("eroded", eroded);
cv::waitKey();
cv::Mat grayscale, thresholded, dilated;
cv::cvtColor(image, grayscale, cv::COLOR_BGR2GRAY);
cv::threshold(grayscale, thresholded, 120, 255, cv::THRESH_BINARY);
cv::dilate(thresholded, dilated, cv::Mat());
cv::imshow("thresholded", thresholded);
cv::imshow("dilated", dilated);
cv::waitKey();
cv::Mat grayscale, thresholded, dilated;
cv::cvtColor(image, grayscale, cv::COLOR_BGR2GRAY);
cv::threshold(grayscale, thresholded, 120, 255, cv::THRESH_BINARY);
auto kernel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(7, 7));
cv::dilate(thresholded, dilated, kernel);
cv::imshow("thresholded", thresholded);
cv::imshow("dilated", dilated);
cv::waitKey();
cv::Mat src_gray, dst;
int threshold_value = 0;
void OnTrackbarValueChanged(int, void*)
{
cv::threshold(src_gray, dst, threshold_value, 255, cv::THRESH_BINARY);
cv::imshow("Threshold", dst);
}
void main()
{
src_gray = cv::imread("C:\\Users\\Matej\\Pictures\\bird.jpg", cv::IMREAD_GRAYSCALE);
cv::namedWindow("Threshold");
cv::createTrackbar("Value", "Threshold", &threshold_value, 255, OnTrackbarValueChanged);
OnTrackbarValueChanged(0, 0);
while (true)
{
if (cv::waitKey(20) == 27)
{
break;
}
}
}
cv::cvtColor(image, grayscale, cv::COLOR_BGR2GRAY);
int histSize = 32; // number of bins
float range[] = { 0, 256 };
const float* histRange = { range };
cv::Mat histogram;
cv::calcHist(&grayscale, 1, 0, cv::Mat(), histogram, 1, &histSize, &histRange);
cv::normalize(histogram, histogram, 0, 1, cv::NORM_MINMAX);
int hist_w = 512, hist_h = 400;
int bin_w = cvRound((double)hist_w / histSize);
cv::Mat histImage(hist_h, hist_w, CV_8UC3, cv::Scalar(0, 0, 0));
for (int i = 0; i < histSize; ++i)
{
cv::line(histImage, cv::Point(i*bin_w, hist_h), cv::Point(i*bin_w, hist_h * (1.0 - histogram.at<float>(i))), cv::Scalar(255, 255, 255));
cv::line(histImage, cv::Point(i*bin_w + bin_w, hist_h * (1.0 - histogram.at<float>(i))), cv::Point(i*bin_w, hist_h * (1.0 - histogram.at<float>(i))), cv::Scalar(255, 255, 255));
cv::line(histImage, cv::Point(i*bin_w + bin_w, hist_h * (1.0 - histogram.at<float>(i))), cv::Point(i*bin_w + bin_w, hist_h), cv::Scalar(255, 255, 255));
}
cv::imshow("Source image", grayscale);
cv::imshow("Histogram ", histImage);
cv::waitKey();
cv::Mat grayscale, filtered;
cv::cvtColor(image, grayscale, cv::COLOR_BGR2GRAY);
cv::Mat kernel = cv::Mat(3, 3, CV_32FC1, cv::Scalar(0));
kernel.ptr<float>(0)[0] = -1.0f;
kernel.ptr<float>(0)[1] = -1.0f;
kernel.ptr<float>(0)[2] = -1.0f;
kernel.ptr<float>(2)[0] = 1.0f;
kernel.ptr<float>(2)[1] = 1.0f;
kernel.ptr<float>(2)[2] = 1.0f;
cv::filter2D(grayscale, filtered, CV_32FC1, kernel);
cv::normalize(filtered, filtered, 0, 1, cv::NORM_MINMAX);
cv::imshow("filtered", filtered);
cv::waitKey();
cv::cvtColor(image, grayscale, cv::COLOR_BGR2GRAY);
cv::Sobel(grayscale, sobel, CV_32FC1, 1, 1);
cv::Laplacian(grayscale, laplacian, CV_32FC1);
cv::normalize(sobel, sobel, 0, 1, cv::NORM_MINMAX);
cv::normalize(laplacian, laplacian, 0, 1, cv::NORM_MINMAX);
cv::imshow("sobel", sobel);
cv::imshow("laplacian", laplacian);
cv::waitKey();
cv::Mat grayscale, thresholded, dilated, contours;
cv::cvtColor(image, grayscale, cv::COLOR_BGR2GRAY);
cv::threshold(grayscale, thresholded, 120, 255, cv::THRESH_BINARY);
cv::dilate(thresholded, dilated, cv::Mat());
cv::bitwise_xor(dilated, thresholded, contours);
cv::imshow("contours", contours);
cv::imshow("thresholded", thresholded);
cv::waitKey();
Program removes contours with area lower than 10.
If your program crashes right at the line with "cv::findContours", you are using wrong version of OpenCV's dll
For Visual Studio 2017 - use the one in folder 'vc15'
For Visual Studio 2015 - use the one in folder 'vc14'
cv::Mat grayscale, thresholded, dilated;
cv::cvtColor(image, grayscale, cv::COLOR_BGR2GRAY);
cv::threshold(grayscale, thresholded, 120, 255, cv::THRESH_BINARY);
cv::imshow("before", thresholded);
std::vector<std::vector<cv::Point>> contoursVector;
cv::findContours(thresholded, contoursVector, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);
cv::imshow("before", thresholded);
thresholded.setTo(0);
for (int i = 0; i < contoursVector.size(); ++i)
{
if (cv::contourArea(contoursVector[i]) > 10)
{
cv::drawContours(thresholded, contoursVector, i, cv::Scalar(255), -1);
}
}
cv::imshow("after", thresholded);
cv::waitKey();
void EraseSmallObjectsContours(cv::Mat& input, cv::Mat& result, double minArea)
{
CV_Assert(input.type() == CV_8UC1);
input.copyTo(result);
std::vector<std::vector<cv::Point>> contoursVector;
cv::findContours(result, contoursVector, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);
input.copyTo(result);
for (size_t i = 0; i < contoursVector.size(); ++i)
{
if (cv::contourArea(contoursVector[i]) < minArea)
{
cv::drawContours(result, contoursVector, i, cv::Scalar(0), -1);
}
}
}
void EraseSmallObjectsFloodFill(cv::Mat& input, cv::Mat& result, double minArea)
{
CV_Assert(input.type() == CV_8UC1);
input.copyTo(result);
for (int y = 0; y < result.rows; ++y)
{
auto ptr = result.ptr(y);
for (int x = 0; x < result.cols; ++x)
{
if (ptr[x] == 255)
{
cv::Rect rect;
cv::floodFill(result, cv::Point(x, y), 128, &rect, cv::Scalar(), cv::Scalar(), 4);
if (rect.area() < minArea)
{
cv::floodFill(result, cv::Point(x, y), 0, &rect, cv::Scalar(), cv::Scalar(), 4);
}
}
}
}
cv::threshold(result, result, 120, 255, cv::THRESH_BINARY);
}
cv::Mat source = cv::imread("D:\\Downloads\\image_bubbles.jpg", cv::IMREAD_GRAYSCALE);
int radius = 10;
cv::Mat maximas;
cv::dilate(source, maximas, cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(2*radius+1, 2*radius+1)));
cv::compare(source, maximas, maximas, cv::CMP_GE);
cv::Mat canvas;
cv::cvtColor(source, canvas, cv::COLOR_GRAY2BGR);
for (int y = 0; y < canvas.rows; ++y)
{
uchar* ptr = maximas.ptr(y);
for (int x = 0; x < canvas.cols; ++x)
{
if (ptr[x] != 0)
{
cv::circle(canvas, cv::Point(x, y), radius, cv::Scalar(0, 0, 255));
}
}
}
cv::imshow("canvas", canvas);
cv::imshow("maximas", maximas);
cv::waitKey();
cv::Mat source = cv::imread("D:\\Downloads\\image_bubbles.jpg", cv::IMREAD_GRAYSCALE);
cv::Mat otsu;
cv::threshold(source, otsu, 0, 255, cv::THRESH_OTSU);
cv::bitwise_not(otsu, otsu);
cv::erode(otsu, otsu, cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(7,7 )));
cv::imshow("mask", otsu);
cv::waitKey();
cv::Mat canvas;
cv::cvtColor(otsu, canvas, cv::COLOR_GRAY2BGR);
std::vector<std::vector<cv::Point>> contours;
cv::findContours(otsu, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
std::vector<std::vector<cv::Point>> hulls(contours.size());
for (int i = 0; i < contours.size(); ++i)
{
double area = cv::contourArea(contours[i]);
if (area < 30)
{}
else
{
cv::convexHull(contours[i], hulls[i]);
double hullArea = cv::contourArea(hulls[i]);
double ratio = area / hullArea;
if (ratio < 0.5)
{
cv::drawContours(canvas, contours, i, cv::Scalar(255, 0, 0), 2);
}
else
{
cv::drawContours(canvas, contours, i, cv::Scalar(0, 255, 0), 2);
cv::drawContours(canvas, hulls, i, cv::Scalar(0, 0, 255), 1);
}
}
}
cv::imshow("canvas", canvas);
cv::waitKey();