毕设之opencv批量生成BMP【圆】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Cracent/article/details/51419226

毕设之opencv批量生成BMP【圆】

程序思路:定义Mat变量,通过circle()函数操作Mat变量进行圆的绘制,再将Mat类型转为IplImage类型,通过cvSaveImage()函数进行BMP格式存储(没有找到简便的Mat变量存储为图片格式的方法)。并将所有图片的绝对地址按序号升序排列存入at.txt中

功能小结:

1、  新建文件夹

    system("md D:\\Circle");

    system(delD:\\filename")

      

2、Mat与IplImage互相转换

将Mat转换为IplImage

举例:    Matimg;

lplImage *src;

                    src=&IplImage(img);

将IplImage转换为Mat

             Mat(const IplImage* img, boolcopyData=false);

3、创建txt文本,并写入数据

#include <iostream>

#include <sstream>

#include <fstream>

ofstream file("D:\\Circle\\at.txt",ios::out);

if (file.is_open())

{

    file << ImagesName;

}

file.close();

4、opencv画圆函数

/**@brief Draws a circle.

Thefunction circle draws a simple or filled circle with a given center and radius.

@paramimg Image where the circle is drawn.

@paramcenter Center of the circle.

@paramradius Radius of the circle.

@paramcolor Circle color.

@paramthickness Thickness of the circle outline, if positive. Negative thicknessmeans that a

filledcircle is to be drawn.

@paramlineType Type of the circle boundary. See the line description.

@paramshift Number of fractional bits in the coordinates of the center and in theradius value.

 */

CV_EXPORTS_W void circle(InputOutputArray img,Point center, int radius,

                       const Scalar& color, int thickness = 1,

                       int lineType =LINE_8, int shift = 0);

源码:

//--------------------------------------【程序说明】-------------------------------------------

//      程序描述:  绘制100个半径随机、圆心随机的圆,并保存为500X500BMP文件

//                  建立“at.txt",包含有所有图片的绝对地址,按序号升序排列

//                  存储位置:D:\\Circle

//                  命名规则:序号 +圆心坐标 +半径

//      开发测试所用操作系统: Windows 7 64bit

//      开发测试所用IDE版本:VisualStudio 2015

//      开发测试所用OpenCV版本:  3.1

//      2015年5月 Created by @Cracent

//      2015年5月 Revised by @Cracent

//------------------------------------------------------------------------------------------------

 

 

//---------------------------------【头文件、命名空间包含部分】----------------------------

//          描述:包含程序所使用的头文件和命名空间

//------------------------------------------------------------------------------------------------

#include <opencv2/core/core.hpp>

#include <opencv2/highgui/highgui.hpp>

 

using namespace cv;

using namespace std;

//此程序对于OpenCV3版需要额外包含头文件:

#include <opencv2/imgproc/imgproc.hpp>

 

#include <iostream>

#include <sstream>

#include <fstream>

 

//-----------------------------------【宏定义部分】--------------------------------------------

//      描述:定义一些辅助宏

//------------------------------------------------------------------------------------------------

#define WINDOW_WIDTH 500                  //定义窗口大小的宏

#define random(x) (rand()%x)              //产生0到x随机数

 

 

//--------------------------------【全局函数声明部分】-------------------------------------

//      描述:全局函数声明

//-----------------------------------------------------------------------------------------------

 

 

//-----------------------------------【ShowpText( )函数】----------------------------------

//          描述:输出一些帮助信息

//----------------------------------------------------------------------------------------------

void ShowText()

{

    //输出程序说明

    printf("绘制100个半径随机、圆心随机的圆,并保存为500X500BMP文件\n");

    printf("建立文本:at.txt,包含有所有图片的绝对地址,按序号升序排列");

    printf("存储位置:D:\\Circle\n");

    printf("命名规则:序号+圆心坐标+半径\n");

    printf("当前使用的OpenCV版本为:"CV_VERSION );

    printf("\n\n ----------------------------------------------------------------------------\n");

}

 

 

 

//---------------------------------------【main( )函数】--------------------------------------

//      描述:控制台应用程序的入口函数,我们的程序从这里开始执行

//-----------------------------------------------------------------------------------------------

int main( void )

{

    ShowText();

    // 创建空白的Mat图像

    Mat CircleImage = Mat::zeros( WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3 );

    //新建文件夹

    system("md D:\\Circle");

    //system(del D:\\filename")

    ofstream file("D:\\Circle\\at.txt",ios::out);

   

 

    //绘制圆

    for (size_t i = 0; i < 100; i++)

    {

        int Circle_x, Circle_y, Circle_r;

        //圆心

        Circle_x = WINDOW_WIDTH / 2 -WINDOW_WIDTH / 10 + random(WINDOW_WIDTH / 5);

        Circle_y = WINDOW_WIDTH / 2 -WINDOW_WIDTH / 10 + random(WINDOW_WIDTH / 5);

        //半径

        Circle_r = random(WINDOW_WIDTH / 5 - 20) +WINDOW_WIDTH / 5;

        //绘制

        CircleImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);

        circle(CircleImage, Point(Circle_x,Circle_y), Circle_r,Scalar(255, 255, 255));

        char ImagesName[1024];

        sprintf(ImagesName, "D:\\Circle\\%d-(%d,%d)%d.bmp\n", i,Circle_x,Circle_y, Circle_r);

        printf(ImagesName);

        if (file.is_open())

        {

            file << ImagesName;

        }

        IplImage *src;

        src = &IplImage(CircleImage);

        cvSaveImage(ImagesName, src);

 

    }

    file.close();

    printf("完成!\n");

    system("pause");

    return(0);

}

 效果图:




猜你喜欢

转载自blog.csdn.net/Cracent/article/details/51419226