当前位置: 首页 > article >正文

绘制纹理C++

用数学和C++绘制一些纹理

sin(x * x + y * y)

int main() {
	int width = 400; // 宽度
	int height = 400; // 高度
	Mat texture = Mat::zeros(height, width, CV_8UC1);
	
	for (int y = 0; y < height; ++y) {
	    for (int x = 0; x < width; ++x) {
	        int value = static_cast<int>(255 * sin(x * x + y * y));
	        texture.at<uchar>(y, x) = static_cast<uchar>(value);
	    }
	}
	
	imshow("Generated Texture", texture);
	imwrite("D:\\doc\\image\\sin(xx+yy).jpg", texture);
	waitKey(0);
    return 0;
}

在这里插入图片描述

交错斜线

 // 生成交错斜线风格的黑白纹理
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            if ((x + y) % 20 < 10) {
                texture.at<uchar>(y, x) = 255; // 白色
            }
            else {
                texture.at<uchar>(y, x) = 0; // 黑色
            }
        }
    }

在这里插入图片描述

竖纹

int width = 400; // 宽度
int height = 400; // 高度

Mat texture = Mat::zeros(height, width, CV_8UC1);

for (int y = 0; y < height; ++y) {
    for (int x = 0; x < width; ++x) {
        int value = static_cast<int>(255 * 0.5 * (1 + sin(2 * M_PI * x / 20)));//竖纹
    }
}

在这里插入图片描述

交错方块

int width = 400; // 宽度
int height = 400; // 高度

Mat texture = Mat::zeros(height, width, CV_8UC1);

for (int y = 0; y < height; ++y) {
    for (int x = 0; x < width; ++x) {
        int value = static_cast<int>(255 * 0.5 * (1 + sin(2 * M_PI * x / 20) * sin(2 * M_PI * y / 20)));
    }
}

在这里插入图片描述

sin(abs(x)) + sin(abs(y))

int width = 400; // 宽度
int height = 400; // 高度

Mat texture = Mat::zeros(height, width, CV_8UC1);

for (int y = 0; y < height; ++y) {
    for (int x = 0; x < width; ++x) {
       int value = static_cast<int>(255 * (sin(abs(x)) + sin(abs(y)))) ;
    }
}

在这里插入图片描述

sin(x)*sin(y)

int width = 400; // 宽度
int height = 400; // 高度

Mat texture = Mat::zeros(height, width, CV_8UC1);

for (int y = 0; y < height; ++y) {
    for (int x = 0; x < width; ++x) {
       int value = static_cast<int>(255 * ( sin(x)*sin(y) )) ;
    }
}

在这里插入图片描述

花纹1

公式 坐标系下的函数: r=sin(n*t)

for (int y = 0; y < height; ++y) {
    for (int x = 0; x < width; ++x) {
        int curx = x % 50;
        int cury = y % 50;
        originX = x/50+25;
        originY = y/50+25;
        float t = atan2(cury - originY, curx - originX);
        float r = sqrt((curx - originX) * (curx - originX) + (cury - originY) * (cury - originY));
        float functionValue = sin(n * t);
        if (abs(r - functionValue * 20) < 5) // 乘以20是为了放大图像
        {
            texture.at<uchar>(y, x) = static_cast<uchar>(200);
        }
    }
}

在这里插入图片描述

花纹2

void drawFunction() {
    // 创建一个空白图像
    int width = 200;
    int height = 200;
    cv::Mat image(height, width, CV_8UC3, cv::Scalar(255, 255, 255));

    // 设置极坐标参数
    int numPoints = max(width, height)*2;
    double radius = min(width, height)/4;

    // 绘制极坐标曲线
    for (int i = 0; i < numPoints; ++i) {
        double angle = 2 * CV_PI * i / numPoints;
        int x = static_cast<int>(radius * (cos(angle) +cos(angle*8)/2 + sin(0*angle) / 3)) + width / 2; // 将坐标平移到图像中心
        int y = static_cast<int>(radius * (sin(angle) +sin(angle*8)/2 + cos(0*angle)/3)) + height / 2; // 将坐标平移到图像中心
        if (x+1 >= 0 && x+1 < width && y+1 >= 0 && y+1 < height) {
            image.at<cv::Vec3b>(y, x) = cv::Vec3b(0, 0, 0); // 绘制像素点
            image.at<cv::Vec3b>(y+1, x) = cv::Vec3b(0, 0, 0); // 绘制像素点
            image.at<cv::Vec3b>(y, x + 1) = cv::Vec3b(0, 0, 0); // 绘制像素点
            image.at<cv::Vec3b>(y + 1, x + 1) = cv::Vec3b(0, 0, 0); // 绘制像素点
        }
    }


    imshow("Generated Texture", image);
    imwrite("D:\\code\\noise\\image\\polar_plot.jpg", image);
    waitKey(0);

}

在这里插入图片描述
https://www.zhihu.com/zvideo/1471926178575970304

叠加

void drawFunction() {
    // 创建一个空白图像
    int width = 200;
    int height = 200;
    cv::Mat image(height, width, CV_8UC3, cv::Scalar(255, 255, 255));

    // 设置极坐标参数
    int numPoints = max(width, height)*4;
    double radius = min(width, height)/4;

    // 绘制极坐标曲线
    for (int i = 0; i < numPoints; ++i) {
        double angle = 2 * CV_PI * i / numPoints;

        
        double tempx = cos(angle) + cos(angle * 8) / 2 + sin(0 * angle) / 3;
        double tempy = sin(angle) + sin(angle * 8) / 2 + cos(0 * angle) / 3;
        int x = static_cast<int>(radius * (tempx)) + width / 2; // 将坐标平移到图像中心
        int y = static_cast<int>(radius * (tempy)) + height / 2; // 将坐标平移到图像中心
        if (x+1 >= 0 && x+1 < width && y+1 >= 0 && y+1 < height) {
            image.at<cv::Vec3b>(y, x) = cv::Vec3b(0, 0, 0); // 绘制像素点
            //image.at<cv::Vec3b>(y+1, x) = cv::Vec3b(0, 0, 0); // 绘制像素点
            //image.at<cv::Vec3b>(y, x + 1) = cv::Vec3b(0, 0, 0); // 绘制像素点
            //image.at<cv::Vec3b>(y + 1, x + 1) = cv::Vec3b(0, 0, 0); // 绘制像素点
        }

        x = static_cast<int>(radius * (tempx)*0.8) + width / 2; // 将坐标平移到图像中心
        y = static_cast<int>(radius * (tempy) * 0.8) + height / 2; // 将坐标平移到图像中心
        if (x + 1 >= 0 && x + 1 < width && y + 1 >= 0 && y + 1 < height) {
            image.at<cv::Vec3b>(y, x) = cv::Vec3b(0, 0, 0); // 绘制像素点
            //image.at<cv::Vec3b>(y + 1, x) = cv::Vec3b(0, 0, 0); // 绘制像素点
            //image.at<cv::Vec3b>(y, x + 1) = cv::Vec3b(0, 0, 0); // 绘制像素点
            //image.at<cv::Vec3b>(y + 1, x + 1) = cv::Vec3b(0, 0, 0); // 绘制像素点
        }

        x = static_cast<int>(radius * (tempx) * 0.618) + width / 2; // 将坐标平移到图像中心
        y = static_cast<int>(radius * (tempy) * 0.618) + height / 2; // 将坐标平移到图像中心
        if (x + 1 >= 0 && x + 1 < width && y + 1 >= 0 && y + 1 < height) {
            image.at<cv::Vec3b>(y, x) = cv::Vec3b(0, 0, 0); // 绘制像素点
            //image.at<cv::Vec3b>(y + 1, x) = cv::Vec3b(0, 0, 0); // 绘制像素点
            //image.at<cv::Vec3b>(y, x + 1) = cv::Vec3b(0, 0, 0); // 绘制像素点
            //image.at<cv::Vec3b>(y + 1, x + 1) = cv::Vec3b(0, 0, 0); // 绘制像素点
        }
        x = static_cast<int>(radius * (tempx) * 0.4) + width / 2; // 将坐标平移到图像中心
        y = static_cast<int>(radius * (tempy) * 0.4) + height / 2; // 将坐标平移到图像中心
        if (x + 1 >= 0 && x + 1 < width && y + 1 >= 0 && y + 1 < height) {
            image.at<cv::Vec3b>(y, x) = cv::Vec3b(0, 0, 0); // 绘制像素点
            //image.at<cv::Vec3b>(y + 1, x) = cv::Vec3b(0, 0, 0); // 绘制像素点
            //image.at<cv::Vec3b>(y, x + 1) = cv::Vec3b(0, 0, 0); // 绘制像素点
            //image.at<cv::Vec3b>(y + 1, x + 1) = cv::Vec3b(0, 0, 0); // 绘制像素点
        }
    }


    imshow("Generated Texture", image);
    imwrite("D:\\code\\noise\\image\\polar_plot.jpg", image);
    waitKey(0);

}

在这里插入图片描述


http://www.kler.cn/news/160202.html

相关文章:

  • Windows 安装 flash-attention 和 bitsandbytes
  • 根文件系统初步测试
  • YOLOv8改进 | TripletAttention三重注意力机制(附代码+机制原理+添加教程)
  • 如何解决syntaxerror: more than 255 arguments 报错
  • HG/T 5367.2-2022 轨道交通车辆耐电弧绝缘涂料检测
  • 论文笔记--A Fine-grained Interpretability Evaluation Benchmark for Neural NLP
  • Redis--13--缓存一致性问题
  • ES 模块语法改为 CommonJS 模块语法的区别
  • RK3288升级WebView版本,替换webview app
  • mv视频怎么做成二维码?扫码用流量看视频更方便
  • 1.2 C语言简介
  • 微服务知识大杂烩
  • uniapp 使用web-view外接三方
  • LeetCode 每日一题 Day 4
  • [leetcode 差分数组] 拼车 M
  • Vue2中v-html引发的安全问题
  • 全息图着色器插件:Hologram Shaders Pro for URP, HDRP Built-in
  • 23 动态规划解买卖股票的最佳时机含手续费
  • node切换版本
  • C++转义符及用法
  • mysql基础之DQL基本单表查询
  • 『Jmeter超级干货』| Linux下Jmeter安装配置、脚本设计执行、监控及报告完整过程
  • Windows 下 PyTorch 入门深度学习环境安装与配置 GPU 版
  • Windows server 部署iSCSI共享磁盘搭建故障转移群集
  • BearPi Std 板从入门到放弃 - 引气入体篇(9)(DAC->ADC)
  • Java LeetCode篇-深入了解二叉树经典解法(三种方式实现:获取二叉树的最大深度)
  • Redis——某马点评day03——part2:秒杀业务异步优化
  • 鸿蒙4.0开发笔记之ArkTS语法基础之应用生命周期与页面中组件的生命周期(十六)
  • Park Unpark
  • Web安全漏洞分析-XSS(下)