記錄一些需要額外實現的小功能模塊,以便之後複製粘貼,2333
1.四個點求解交點
cv::Point2f cross_Points(std::vector<cv::Point2f>&points) {
// cross point
cv::Point2f cross_point;
double y2_4 = points[1].y - points[3].y;
double x1_3 = points[0].x - points[2].x;
double y1_3 = points[0].y - points[2].y;
double x2_4 = points[1].x - points[3].x;
double y1_2 = points[0].y - points[1].y;
double A = y2_4 * x1_3 - y1_3 * x2_4;
std::cout << A << std::endl;
if (A == 0)
return cv::Point2f(0,0);
double B = y2_4 * x1_3*points[1].x - y1_3 * x2_4*points[0].x + y1_2 * x2_4*x1_3;
std::cout << B << std::endl;
cross_point.x = B / A;
cross_point.y = y1_3 / x1_3 * (cross_point.x - points[0].x) + points[0].y;
std::cout << "交點座標" << cross_point.x << " " << cross_point.y << std::endl;
return cross_point;
}
2.二維旋轉座標計算
(opencv好像只提供了整幅圖像的矯正)
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
cv::Point2f rotation_point2f(cv::Mat & rotation, cv::Point2f & point) {
cv::Point2f result;
result.x = rotation.at<double>(0, 0) * point.x +
rotation.at<double>(0, 1) * point.y +
rotation.at<double>(0, 2);
result.y = rotation.at<double>(1, 0) * point.x +
rotation.at<double>(1, 1) * point.y +
rotation.at<double>(1, 2);
return result;
}
int main() {
double x = 1;
double y = 1;
double angle = x == 0 ? 0 : atan(y / x);
cv::Mat rotation = cv::getRotationMatrix2D(cv::Point2f(50,50), angle / 3.14 * 180, 1.0);
cv::Point2f point(50, 50);
std::cout << rotation_point2f(rotation,point);
std::system("pause");
}
3.繪製中文
opencv“繪製”中文漢字(僅限windows),效果如圖:(無需typefree,會調用系統的中文字庫)
opencv的putText不支持中文衆所周知,之前寫了一篇利用freetype“畫”中文的文章太過冗餘,下面這種方法在windows更方便。
#include <opencv2/opencv.hpp>
#include <windows.h>
#include <string>
using namespace cv;
void GetStringSize(HDC hDC, const char* str, int* w, int* h);
void putTextZH(cv::Mat &dst, const char* str, cv::Point org, cv::Scalar color, int fontSize,
const char *fn = "Arial", bool italic = false, bool underline = false);
int main()
{
cv::Mat img = cv::imread("1.jpg");
const char *msg = "最英俊的人";
putTextZH(img, msg, cv::Point(50, 50), cv::Scalar(0, 0, 255), 20);
imshow("最英俊的人", img);
cv::waitKey(-1);
return 0;
}
void GetStringSize(HDC hDC, const char* str, int* w, int* h)
{
SIZE size;
GetTextExtentPoint32A(hDC, str, strlen(str), &size);
if (w != 0) *w = size.cx;
if (h != 0) *h = size.cy;
}
void putTextZH(Mat &dst, const char* str, Point org, Scalar color, int fontSize, const char* fn, bool italic, bool underline)
{
CV_Assert(dst.data != 0 && (dst.channels() == 1 || dst.channels() == 3));
int x, y, r, b;
if (org.x > dst.cols || org.y > dst.rows) return;
x = org.x < 0 ? -org.x : 0;
y = org.y < 0 ? -org.y : 0;
LOGFONTA lf;
lf.lfHeight = -fontSize;
lf.lfWidth = 0;
lf.lfEscapement = 0;
lf.lfOrientation = 0;
lf.lfWeight = 5;
lf.lfItalic = italic; //斜體
lf.lfUnderline = underline; //下劃線
lf.lfStrikeOut = 0;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfOutPrecision = 0;
lf.lfClipPrecision = 0;
lf.lfQuality = PROOF_QUALITY;
lf.lfPitchAndFamily = 0;
strcpy_s(lf.lfFaceName, fn);
HFONT hf = CreateFontIndirectA(&lf);
HDC hDC = CreateCompatibleDC(0);
HFONT hOldFont = (HFONT)SelectObject(hDC, hf);
int strBaseW = 0, strBaseH = 0;
int singleRow = 0;
char buf[1 << 12];
strcpy_s(buf, str);
char *bufT[1 << 12]; // 這個用於分隔字符串後剩餘的字符,可能會超出。
//處理多行
{
int nnh = 0;
int cw, ch;
const char* ln = strtok_s(buf, "\n", bufT);
while (ln != 0)
{
GetStringSize(hDC, ln, &cw, &ch);
strBaseW = max(strBaseW, cw);
strBaseH = max(strBaseH, ch);
ln = strtok_s(0, "\n", bufT);
nnh++;
}
singleRow = strBaseH;
strBaseH *= nnh;
}
if (org.x + strBaseW < 0 || org.y + strBaseH < 0)
{
SelectObject(hDC, hOldFont);
DeleteObject(hf);
DeleteObject(hDC);
return;
}
r = org.x + strBaseW > dst.cols ? dst.cols - org.x - 1 : strBaseW - 1;
b = org.y + strBaseH > dst.rows ? dst.rows - org.y - 1 : strBaseH - 1;
org.x = org.x < 0 ? 0 : org.x;
org.y = org.y < 0 ? 0 : org.y;
BITMAPINFO bmp = { 0 };
BITMAPINFOHEADER& bih = bmp.bmiHeader;
int strDrawLineStep = strBaseW * 3 % 4 == 0 ? strBaseW * 3 : (strBaseW * 3 + 4 - ((strBaseW * 3) % 4));
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biWidth = strBaseW;
bih.biHeight = strBaseH;
bih.biPlanes = 1;
bih.biBitCount = 24;
bih.biCompression = BI_RGB;
bih.biSizeImage = strBaseH * strDrawLineStep;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
void* pDibData = 0;
HBITMAP hBmp = CreateDIBSection(hDC, &bmp, DIB_RGB_COLORS, &pDibData, 0, 0);
CV_Assert(pDibData != 0);
HBITMAP hOldBmp = (HBITMAP)SelectObject(hDC, hBmp);
//color.val[2], color.val[1], color.val[0]
SetTextColor(hDC, RGB(255, 255, 255));
SetBkColor(hDC, 0);
//SetStretchBltMode(hDC, COLORONCOLOR);
strcpy_s(buf, str);
const char* ln = strtok_s(buf, "\n", bufT);
int outTextY = 0;
while (ln != 0)
{
TextOutA(hDC, 0, outTextY, ln, strlen(ln));
outTextY += singleRow;
ln = strtok_s(0, "\n", bufT);
}
uchar* dstData = (uchar*)dst.data;
int dstStep = dst.step / sizeof(dstData[0]);
unsigned char* pImg = (unsigned char*)dst.data + org.x * dst.channels() + org.y * dstStep;
unsigned char* pStr = (unsigned char*)pDibData + x * 3;
for (int tty = y; tty <= b; ++tty)
{
unsigned char* subImg = pImg + (tty - y) * dstStep;
unsigned char* subStr = pStr + (strBaseH - tty - 1) * strDrawLineStep;
for (int ttx = x; ttx <= r; ++ttx)
{
for (int n = 0; n < dst.channels(); ++n) {
double vtxt = subStr[n] / 255.0;
int cvv = vtxt * color.val[n] + (1 - vtxt) * subImg[n];
subImg[n] = cvv > 255 ? 255 : (cvv < 0 ? 0 : cvv);
}
subStr += 3;
subImg += dst.channels();
}
}
SelectObject(hDC, hOldBmp);
SelectObject(hDC, hOldFont);
DeleteObject(hf);
DeleteObject(hBmp);
DeleteDC(hDC);
}
4.超像素分割
效果如圖,就問你怕不怕,233,原理可以自行百度,大家感興趣的話我也可以寫一篇
main函數:
#include <opencv2/opencv.hpp>
#include <vector>
#include "slic.h"
using namespace std;
int main() {
/* Load the image and convert to Lab colour space. */
cv::Mat image = cv::imread("1.jpg");
cv::Mat lab_image = image.clone();
cv::cvtColor(image, lab_image, CV_BGR2Lab);
/* Yield the number of superpixels and weight-factors from the user. */
int w = image.cols, h = image.rows;
int nr_superpixels = 100;
int nc = 40;
double step = sqrt((w * h) / (double)nr_superpixels);
/* Perform the SLIC superpixel algorithm. */
Slic slic;
slic.generate_superpixels(&(IplImage)lab_image, step, nc);
slic.create_connectivity(&(IplImage)lab_image);
/* Display the contours and show the result. */
//slic.colour_with_cluster_means(&(IplImage)image);
slic.display_center_grid(&(IplImage)image, cv::Scalar(0, 0, 255));
slic.display_contours(&(IplImage)image, cv::Scalar(0, 0, 255));
cv::imshow("result", image);
cv::waitKey();
system("pause");
}
slic.h
#ifndef SLIC_H
#define SLIC_H
/* slic.h.
*
* Written by: Pascal Mettes.
*
* This file contains the class elements of the class Slic. This class is an
* implementation of the SLIC Superpixel algorithm by Achanta et al. [PAMI'12,
* vol. 34, num. 11, pp. 2274-2282].
*
* This implementation is created for the specific purpose of creating
* over-segmentations in an OpenCV-based environment.
*/
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <float.h>
using namespace std;
/* 2d matrices are handled by 2d vectors. */
#define vec2dd vector<vector<double> >
#define vec2di vector<vector<int> >
#define vec2db vector<vector<bool> >
/* The number of iterations run by the clustering algorithm. */
#define NR_ITERATIONS 10
/*
* class Slic.
*
* In this class, an over-segmentation is created of an image, provided by the
* step-size (distance between initial cluster locations) and the colour
* distance parameter.
*/
class Slic {
private:
/* The cluster assignments and distance values for each pixel. */
vec2di clusters;
vec2dd distances;
/* The LAB and xy values of the centers. */
vec2dd centers;
/* The number of occurences of each center. */
vector<int> center_counts;
/* The step size per cluster, and the colour (nc) and distance (ns)
* parameters. */
int step, nc, ns;
/* Compute the distance between a center and an individual pixel. */
double compute_dist(int ci, CvPoint pixel, CvScalar colour);
/* Find the pixel with the lowest gradient in a 3x3 surrounding. */
CvPoint find_local_minimum(IplImage *image, CvPoint center);
/* Remove and initialize the 2d vectors. */
void clear_data();
void init_data(IplImage *image);
public:
/* Class constructors and deconstructors. */
Slic();
~Slic();
/* Generate an over-segmentation for an image. */
void generate_superpixels(IplImage *image, int step, int nc);
/* Enforce connectivity for an image. */
void create_connectivity(IplImage *image);
/* Draw functions. Resp. displayal of the centers and the contours. */
void display_center_grid(IplImage *image, CvScalar colour);
void display_contours(IplImage *image, CvScalar colour);
void colour_with_cluster_means(IplImage *image);
};
#endif
slic.cpp
#include "slic.h"
/*
* Constructor. Nothing is done here.
*/
Slic::Slic() {
}
/*
* Destructor. Clear any present data.
*/
Slic::~Slic() {
clear_data();
}
/*
* Clear the data as saved by the algorithm.
*
* Input : -
* Output: -
*/
void Slic::clear_data() {
clusters.clear();
distances.clear();
centers.clear();
center_counts.clear();
}
/*
* Initialize the cluster centers and initial values of the pixel-wise cluster
* assignment and distance values.
*
* Input : The image (IplImage*).
* Output: -
*/
void Slic::init_data(IplImage *image) {
/* Initialize the cluster and distance matrices. */
for (int i = 0; i < image->width; i++) {
vector<int> cr;
vector<double> dr;
for (int j = 0; j < image->height; j++) {
cr.push_back(-1);
dr.push_back(FLT_MAX);
}
clusters.push_back(cr);
distances.push_back(dr);
}
/* Initialize the centers and counters. */
for (int i = step; i < image->width - step/2; i += step) {
for (int j = step; j < image->height - step/2; j += step) {
vector<double> center;
/* Find the local minimum (gradient-wise). */
CvPoint nc = find_local_minimum(image, cvPoint(i,j));
CvScalar colour = cvGet2D(image, nc.y, nc.x);
/* Generate the center vector. */
center.push_back(colour.val[0]);
center.push_back(colour.val[1]);
center.push_back(colour.val[2]);
center.push_back(nc.x);
center.push_back(nc.y);
/* Append to vector of centers. */
centers.push_back(center);
center_counts.push_back(0);
}
}
}
/*
* Compute the distance between a cluster center and an individual pixel.
*
* Input : The cluster index (int), the pixel (CvPoint), and the Lab values of
* the pixel (CvScalar).
* Output: The distance (double).
*/
double Slic::compute_dist(int ci, CvPoint pixel, CvScalar colour) {
double dc = sqrt(pow(centers[ci][0] - colour.val[0], 2) + pow(centers[ci][1]
- colour.val[1], 2) + pow(centers[ci][2] - colour.val[2], 2));
double ds = sqrt(pow(centers[ci][3] - pixel.x, 2) + pow(centers[ci][4] - pixel.y, 2));
return sqrt(pow(dc / nc, 2) + pow(ds / ns, 2));
//double w = 1.0 / (pow(ns / nc, 2));
//return sqrt(dc) + sqrt(ds * w);
}
/*
* Find a local gradient minimum of a pixel in a 3x3 neighbourhood. This
* method is called upon initialization of the cluster centers.
*
* Input : The image (IplImage*) and the pixel center (CvPoint).
* Output: The local gradient minimum (CvPoint).
*/
CvPoint Slic::find_local_minimum(IplImage *image, CvPoint center) {
double min_grad = FLT_MAX;
CvPoint loc_min = cvPoint(center.x, center.y);
for (int i = center.x-1; i < center.x+2; i++) {
for (int j = center.y-1; j < center.y+2; j++) {
CvScalar c1 = cvGet2D(image, j+1, i);
CvScalar c2 = cvGet2D(image, j, i+1);
CvScalar c3 = cvGet2D(image, j, i);
/* Convert colour values to grayscale values. */
double i1 = c1.val[0];
double i2 = c2.val[0];
double i3 = c3.val[0];
/*double i1 = c1.val[0] * 0.11 + c1.val[1] * 0.59 + c1.val[2] * 0.3;
double i2 = c2.val[0] * 0.11 + c2.val[1] * 0.59 + c2.val[2] * 0.3;
double i3 = c3.val[0] * 0.11 + c3.val[1] * 0.59 + c3.val[2] * 0.3;*/
/* Compute horizontal and vertical gradients and keep track of the
minimum. */
if (sqrt(pow(i1 - i3, 2)) + sqrt(pow(i2 - i3,2)) < min_grad) {
min_grad = fabs(i1 - i3) + fabs(i2 - i3);
loc_min.x = i;
loc_min.y = j;
}
}
}
return loc_min;
}
/*
* Compute the over-segmentation based on the step-size and relative weighting
* of the pixel and colour values.
*
* Input : The Lab image (IplImage*), the stepsize (int), and the weight (int).
* Output: -
*/
void Slic::generate_superpixels(IplImage *image, int step, int nc) {
this->step = step;
this->nc = nc;
this->ns = step;
/* Clear previous data (if any), and re-initialize it. */
clear_data();
init_data(image);
/* Run EM for 10 iterations (as prescribed by the algorithm). */
for (int i = 0; i < NR_ITERATIONS; i++) {
/* Reset distance values. */
for (int j = 0; j < image->width; j++) {
for (int k = 0;k < image->height; k++) {
distances[j][k] = FLT_MAX;
}
}
for (int j = 0; j < (int) centers.size(); j++) {
/* Only compare to pixels in a 2 x step by 2 x step region. */
for (int k = centers[j][3] - step; k < centers[j][3] + step; k++) {
for (int l = centers[j][4] - step; l < centers[j][4] + step; l++) {
if (k >= 0 && k < image->width && l >= 0 && l < image->height) {
CvScalar colour = cvGet2D(image, l, k);
double d = compute_dist(j, cvPoint(k,l), colour);
/* Update cluster allocation if the cluster minimizes the
distance. */
if (d < distances[k][l]) {
distances[k][l] = d;
clusters[k][l] = j;
}
}
}
}
}
/* Clear the center values. */
for (int j = 0; j < (int) centers.size(); j++) {
centers[j][0] = centers[j][1] = centers[j][2] = centers[j][3] = centers[j][4] = 0;
center_counts[j] = 0;
}
/* Compute the new cluster centers. */
for (int j = 0; j < image->width; j++) {
for (int k = 0; k < image->height; k++) {
int c_id = clusters[j][k];
if (c_id != -1) {
CvScalar colour = cvGet2D(image, k, j);
centers[c_id][0] += colour.val[0];
centers[c_id][1] += colour.val[1];
centers[c_id][2] += colour.val[2];
centers[c_id][3] += j;
centers[c_id][4] += k;
center_counts[c_id] += 1;
}
}
}
/* Normalize the clusters. */
for (int j = 0; j < (int) centers.size(); j++) {
centers[j][0] /= center_counts[j];
centers[j][1] /= center_counts[j];
centers[j][2] /= center_counts[j];
centers[j][3] /= center_counts[j];
centers[j][4] /= center_counts[j];
}
}
}
/*
* Enforce connectivity of the superpixels. This part is not actively discussed
* in the paper, but forms an active part of the implementation of the authors
* of the paper.
*
* Input : The image (IplImage*).
* Output: -
*/
void Slic::create_connectivity(IplImage *image) {
int label = 0, adjlabel = 0;
const int lims = (image->width * image->height) / ((int)centers.size());
const int dx4[4] = {-1, 0, 1, 0};
const int dy4[4] = { 0, -1, 0, 1};
/* Initialize the new cluster matrix. */
vec2di new_clusters;
for (int i = 0; i < image->width; i++) {
vector<int> nc;
for (int j = 0; j < image->height; j++) {
nc.push_back(-1);
}
new_clusters.push_back(nc);
}
for (int i = 0; i < image->width; i++) {
for (int j = 0; j < image->height; j++) {
if (new_clusters[i][j] == -1) {
vector<CvPoint> elements;
elements.push_back(cvPoint(i, j));
/* Find an adjacent label, for possible use later. */
for (int k = 0; k < 4; k++) {
int x = elements[0].x + dx4[k], y = elements[0].y + dy4[k];
if (x >= 0 && x < image->width && y >= 0 && y < image->height) {
if (new_clusters[x][y] >= 0) {
adjlabel = new_clusters[x][y];
}
}
}
int count = 1;
for (int c = 0; c < count; c++) {
for (int k = 0; k < 4; k++) {
int x = elements[c].x + dx4[k], y = elements[c].y + dy4[k];
if (x >= 0 && x < image->width && y >= 0 && y < image->height) {
if (new_clusters[x][y] == -1 && clusters[i][j] == clusters[x][y]) {
elements.push_back(cvPoint(x, y));
new_clusters[x][y] = label;
count += 1;
}
}
}
}
/* Use the earlier found adjacent label if a segment size is
smaller than a limit. */
if (count <= lims >> 2) {
for (int c = 0; c < count; c++) {
new_clusters[elements[c].x][elements[c].y] = adjlabel;
}
label -= 1;
}
label += 1;
}
}
}
}
/*
* Display the cluster centers.
*
* Input : The image to display upon (IplImage*) and the colour (CvScalar).
* Output: -
*/
void Slic::display_center_grid(IplImage *image, CvScalar colour) {
for (int i = 0; i < (int) centers.size(); i++) {
cvCircle(image, cvPoint(centers[i][3], centers[i][4]), 2, colour, 2);
}
}
/*
* Display a single pixel wide contour around the clusters.
*
* Input : The target image (IplImage*) and contour colour (CvScalar).
* Output: -
*/
void Slic::display_contours(IplImage *image, CvScalar colour) {
const int dx8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dy8[8] = { 0, -1, -1, -1, 0, 1, 1, 1};
/* Initialize the contour vector and the matrix detailing whether a pixel
* is already taken to be a contour. */
vector<CvPoint> contours;
vec2db istaken(image->width,vector<bool>(image->height,false));
//for (int i = 0; i < image->width; i++) {
// vector<bool> nb;
// for (int j = 0; j < image->height; j++) {
// nb.push_back(false);
// }
// istaken.push_back(nb);
// }
/* Go through all the pixels. */
cv::Mat showMat( clusters[0].size(), clusters.size(), CV_8UC1,cv::Scalar(0));
for (int i = 0; i < image->width; i++) {
for (int j = 0; j < image->height; j++) {
showMat.at<uchar>(j, i) = clusters[i][j] * 2;
}
}
cv::imshow("showMat", showMat);
for (int i = 0; i < image->width; i++) {
for (int j = 0; j < image->height; j++) {
int nr_p = 0;
/* Compare the pixel to its 8 neighbours. */
for (int k = 0; k < 8; k++) {
int x = i + dx8[k], y = j + dy8[k];
if (x >= 0 && x < image->width && y >= 0 && y < image->height) {
if (istaken[x][y] == false && clusters[i][j] != clusters[x][y]) {
nr_p += 1;
}
}
}
/* Add the pixel to the contour list if desired. */
if (nr_p >= 2) {
contours.push_back(cvPoint(i,j));
istaken[i][j] = true;
}
}
}
/* Draw the contour pixels. */
for (int i = 0; i < (int)contours.size(); i++) {
cvSet2D(image, contours[i].y, contours[i].x, colour);
}
std::cout << 233 << std::endl;
}
/*
* Give the pixels of each cluster the same colour values. The specified colour
* is the mean RGB colour per cluster.
*
* Input : The target image (IplImage*).
* Output: -
*/
void Slic::colour_with_cluster_means(IplImage *image) {
vector<CvScalar> colours(centers.size());
/* Gather the colour values per cluster. */
for (int i = 0; i < image->width; i++) {
for (int j = 0; j < image->height; j++) {
int index = clusters[i][j];
CvScalar colour = cvGet2D(image, j, i);
colours[index].val[0] += colour.val[0];
colours[index].val[1] += colour.val[1];
colours[index].val[2] += colour.val[2];
}
}
/* Divide by the number of pixels per cluster to get the mean colour. */
for (int i = 0; i < (int)colours.size(); i++) {
colours[i].val[0] /= center_counts[i];
colours[i].val[1] /= center_counts[i];
colours[i].val[2] /= center_counts[i];
}
/* Fill in. */
for (int i = 0; i < image->width; i++) {
for (int j = 0; j < image->height; j++) {
CvScalar ncolour = colours[clusters[i][j]];
cvSet2D(image, j, i, ncolour);
}
}
}