惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
H
Help Net Security
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
量子位
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
AI
AI
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
U
Unit 42
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
Schneier on Security
Schneier on Security
博客园 - Franky
H
Heimdal Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
N
News | PayPal Newsroom
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
雷峰网
雷峰网

博客园 - double64

C++ 安全的拷贝赋值(Copy-and-Swap 惯用法) C++ 基类派生类的 static_cast 和 dynamic_cast 转换的简单测试 visual studio 的 snippet 代码片段模板样式 Windows 右键管理官方小程序Autoruns C++ 结合 enum 按位与或组合检测枚举项 std::unique_ptr 当删除器类型为 无状态的时构造可以不用传删除器示例 两个用来写 CLI - Command-Line Interface 的命令解析库 enum class 类型转换 int 微软输入法中如何输出当前时间 音程知识 C++ 模板引用参数的各种情况 英语 12 种时态 Qt 手动添加 Q_OBJECT 需要添加的地方 C# WPF 绑定 ObservableObject 实现 INotifyPropertyChanged 接口 C++ 标准库 copy_if C++ lambda 和 bind 何时优先使用 Windows 下的 Qt 中 min max 函数冲突 C++ RVO 或 NRVO 可能触发的条件 C++ std::unique_ptr 和 std::shared_ptr 都支持自定义删除器(deleter) C++ 智能指针和动态数组 std::vector 插入另一个 vector 的范围元素 Qt tableWidget QTableWidget 常用一些属性设置 C++ 内部类(嵌套类)是可以访问外部类的私有保护成员的 C++ 宏展开顺序 C++ std::round() 四舍五入 简单易用的图像库:stb_image Halcon HImage 与 Qt QImage 的相互转换
cv::Mat 和 HalconCpp::HImage 生成和保存图像简单测试
double64 · 2025-04-12 · via 博客园 - double64
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include <iostream>

//using namespace cv;
//using namespace std;


cv::Mat GenMatImg()
{
	// 设置图片尺寸
	const int width = 400;
	const int height = 300;

	// 创建一个空白RGB图像(3通道)
	cv::Mat image(height, width, CV_8UC3, cv::Scalar(0, 0, 0));

	// 填充颜色 - 创建渐变效果
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			// 设置RGB值(注意OpenCV使用BGR顺序)
			int b = 128;  // 蓝色固定值
			int g = static_cast<int>(255 * y / static_cast<float>(height));  // 绿色从上到下渐变
			int r = static_cast<int>(255 * x / static_cast<float>(width));  // 红色从左到右渐变

			image.at<cv::Vec3b>(y, x) = cv::Vec3b(b, g, r);
		}
	}

	// 在图像上添加文字
	cv::String text = "OpenCV RGB Image";
	cv::Point textOrg(50, 150);
	int fontFace = cv::FONT_HERSHEY_SIMPLEX;
	double fontScale = 0.8;
	cv::Scalar color(255, 255, 255);  // 白色文字
	int thickness = 2;
	cv::putText(image, text, textOrg, fontFace, fontScale, color, thickness, cv::LINE_AA);
 
	return image;
}

bool SaveMat(cv::Mat &image, const cv::String &title)
{
	// 保存图像到文件
	//cv::String outputFilename = "generated_rgb_image.jpg";
	bool isSuccess = imwrite(title, image);
	if (isSuccess) 
	{
		std::cout << "图像已成功保存为 " << title << std::endl;
		return true;
	}
	else 
	{
		std::cerr << "保存图像失败!" << std::endl;
		return false;
	}
}


#include "Halcon.h"
#include "HalconCpp/HalconCpp.h"
#include <iostream>

using namespace HalconCpp;
using namespace std;

HImage GenHImage()
{
	try {
		// 设置图片尺寸
		const int width = 400;
		const int height = 300;

		// 创建三个单通道图像分别代表R、G、B
		HImage imgR, imgG, imgB;

		// 生成红色分量(从左到右渐变)
		HalconCpp::GenImageConst(&imgR, "byte", width, height);
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				int r = static_cast<int>(255 * x / static_cast<float>(width));
				imgR.SetGrayval(y, x, r);
			}
		}

		// 生成绿色分量(从上到下渐变)
		HalconCpp::GenImageConst(&imgG, "byte", width, height);
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				int g = static_cast<int>(255 * y / static_cast<float>(height));
				imgG.SetGrayval(y, x, g);
			}
		}

		// 生成蓝色分量(固定值128)
		HalconCpp::GenImageConst(&imgB, "byte", width, height);
		imgB = imgB + 128;

		// 将三个单通道图像合并为RGB图像
		HImage rgbImage;
		HalconCpp::Compose3(imgR, imgG, imgB, &rgbImage);

		// 在图像上添加文字
		HTuple widthText, heightText;
		rgbImage.GetImageSize(&widthText, &heightText);
		HTuple row = heightText / 2.0;
		HTuple column = widthText / 4.0;

		HWindow window;
		window.OpenWindow(0, 0, width, height, 0, "visible", "");
		window.DispObj(rgbImage);
		HalconCpp::DispText(window, "Halcon RGB Image", "image", row, column, "white", "box", "false");

		// 从窗口获取带有文字的图像
		HImage imageWithText = window.DumpWindowImage();
 
		return imageWithText;
	}
	catch (HException& ex) {
		std::cerr << "Halcon异常: " << ex.ErrorMessage().Text() << endl;
		return HImage();
	}

	return HImage();
}

bool SaveHImg(HalconCpp::HObject image, const char *title)
{
	HalconCpp::HImage hImg(image);
	hImg.WriteImage("bmp", 0, title);
	std::cout << "保存 HImage 完成!" << std::endl;
	return true;
}

#include "visioncomm/IImagePr.h"

int main()
{
	cv::Mat genmat = GenMatImg();
	SaveMat(genmat, "Mat_Image.jpg");

	vsc::PrImage primg = genmat;
	HalconCpp::HObject cnvMatToHobj = primg;
	SaveHImg(cnvMatToHobj, "Mat_Image_cnvMatToHobj");


	HalconCpp::HObject genhImg = GenHImage();
	SaveHImg(genhImg, "HImage");
	primg = genhImg;
	cv::Mat cnvHObjToMat = primg;
	SaveMat(cnvHObjToMat, "HImage_cnvHObjToMat.jpg");

	system("pause");
	return 0;
}