platform:opencv 3.4.2&vs2017
主函数:
#include <iostream>
using namespace std;
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
#include "area_deal.h"
int main() {
cvNamedWindow("dog", CV_WINDOW_AUTOSIZE);
IplImage* dog = cvLoadImage("..//..//dog.bmp");
area_deal GB_max;
GB_max.GB_max(dog);
cvShowImage("dog", dog);
cvWaitKey(0);
/*区域ROI*/
GB_max.Roi(*dog);
/*区域WidthStep*/
GB_max.WidthStep(*dog);
/*添加字*/
GB_max.font_plus(*dog);
return 0;
}
增加类:
#pragma once
#include <iostream>
using namespace std;
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <cv.h>
#include<highgui.h>
using namespace cv;
class area_deal
{
public:
area_deal();
~area_deal();
void GB_max(IplImage * in);
void Roi(IplImage in);
void WidthStep(IplImage in);
void font_plus(IplImage in);
};
#include "area_deal.h"
area_deal::area_deal()
{
}
area_deal::~area_deal()
{
}
void area_deal::GB_max(IplImage * in) {
for (int row = 0; row < in->height; row++) {
uchar * ptr = (uchar *)(in->imageData + row * in->widthStep);
for (int col = 0; col < in->width; col++) {
if(ptr[3*col+1]<235) ptr[3 * col + 1] += 20;
if(ptr[3*col+2]<235) ptr[3 * col + 2] += 20;
}
}
}
void area_deal::Roi(IplImage in) {
cvNamedWindow("area_deal_ROI", CV_WINDOW_AUTOSIZE);
cvSetImageROI(&in, CvRect(100, 100, 50, 50));
//CvScalar通道顺序是B G R+透明度
cvAddS(&in, CvScalar(50), &in);
cvResetImageROI(&in);
cvShowImage("area_deal_ROI", &in);
cvWaitKey(0);
cvDestroyWindow("area_deal_ROI");
}
void area_deal::WidthStep(IplImage in) {
cvNamedWindow("area_deal_WidthStep", CV_WINDOW_AUTOSIZE);
CvRect in_img = CvRect(200, 200, 100, 100);
IplImage * sub_img = cvCreateImageHeader(CvSize(
in_img.width,
in_img.height),
in.depth, in.nChannels);
/*origin有两种取值 IPL_ORIGIN_TL与IPL_ORIGIN_BL分别表示坐标原点位于图像的左上角
或则左下角*/
sub_img->origin = in.origin;
sub_img->widthStep = in.widthStep;
/*sub_img的数据指针指向in图像的(200,200)位置*/
sub_img->imageData = in.imageData + in_img.width*in.widthStep + in_img.height*in.nChannels;
cvAddS(sub_img, cvScalar(100), sub_img);
cvReleaseImageHeader(&sub_img);
cvShowImage("area_deal_WidthStep", &in);
cvWaitKey(0);
cvDestroyWindow("area_deal_WidthStep");
}
void area_deal::font_plus(IplImage in) {
cvNamedWindow("font_plus", CV_WINDOW_AUTOSIZE);
CvFont word;
cvInitFont(&word, CV_FONT_HERSHEY_COMPLEX_SMALL,
1, 1, 1.0, 1, 8);
cvPutText(&in, "kris'dog", CvPoint(in.width*0.8,380), &word, CvScalar(0, 100));
Mat ad = cvarrToMat(&in);
cvShowImage("font_plus", &in);
cvWaitKey(0);
cvDestroyWindow("font_plus");
imwrite("..//..//area_deal.bmp", ad);
}