我从文本框中读取路径,然后尝试打开图像
String^ P = path->Text;
IplImage* img = cvLoadImage(P);
它给了我以下错误
Error 1 error C2664: 'cvLoadImage' : cannot convert parameter 1 from 'System::String ^' to 'const char *'
谁能告诉我如何将其转换为 char *.
System::String ^ str = path->Text ;
char* str2 = (char*)Marshal::StringToHGlobalAnsi(str).ToPointer();
printf(str2);
包括以下头文件
#using <mscorlib.dll>
using namespace System::Runtime::InteropServices;
永远不会太迟。
这是我正在使用的代码
并且不要忘记将头文件包含在基本文件#include“saveImg.h”中调用它
保存图片 () ;
保存图片
int saveImg (){ // int argc , char* argv[] ) { // (int argc, char** argv) {
IplImage* img;
// if( argc == 2 && (img=cvLoadImage(argv[1], 1))!= 0) {
img=cvLoadImage("hello.jpg") ;
IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 );
IplImage* net1 = cvCreateImage( cvGetSize(img), 8, 3 );
CvMemStorage* storage = cvCreateMemStorage(0);
cvCvtColor( img, gray, CV_BGR2GRAY );
cvSmooth( gray, gray, CV_GAUSSIAN, 9, 9 ); // smooth it, otherwise a lot of false circles may be detected
CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, gray->height/4, 200, 100 );
int i;
for( i = 0; i < circles->total; i++ ) {
float* p = (float*)cvGetSeqElem( circles, i );
cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), 3, CV_RGB(0,255,0), -1, 8, 0 );
cvCircle( img, cvPoint(cvRound(p[1]),cvRound(p[2])), cvRound(p[3]), CV_RGB(50,0,100), 3, 8, 0 );
// cvWaitKey(0);
}
cvNamedWindow( "Original", 1 );
cvShowImage( "Original", img );
cvWaitKey(0);
cvNamedWindow( "circles", 1 );
cvShowImage( "circles", gray );
cvWaitKey(0);
return 0;
}
在 Visual Studio C++ 10 及更高版本中将字符 * 转换为字符串 ^
我在制作一个小型计算器时测试了这个并且OK....
// Variables :
char * number1 ;
double num1 ;
String ^ no1 ;
number1 = (char*)Marshal::StringToHGlobalAnsi(no1).ToPointer(); num1 = atof (number1) ;