Ubuntu下用C编写PHP扩展 例子展示 绝对详细

安装php5-dev

 

Php代码   收藏代码
  1. sudo apt-get install php5-dev  

  下载PHP源码

 

Php代码   收藏代码
  1. sudo apt-get source php5  

 创建模块模型

 

Php代码   收藏代码
  1. cd ./php-5.3.2/ext  
  2. ./ext_skel --extname=roger  
 



 

进入roger目录,这里主要编辑的文件有两个:config.m4和roger.c,config.m4可以配置扩展编译进php的方法,roger.c是编码模块的主要文件。使用vim编辑config.m4文件,找到以下几行:



 改变为:



 退出保存(roger.c暂时不做修改);

执行命令phpize,phpize是用来扩展php模块的,完成后可以看到产生了./configure程序:



 安装

 

Php代码   收藏代码
  1. ./configure --with-php-config=/usr/bin/php-config  
  2. make  
  3. make install  
 


查看生成的roger.so:


修改php.ini加载roger.so,重启apache;

 

查看phpinfo(),可以看到roger.so已经加载:



 创建一个php文件,写入:



 运行结果:


============================== 自定义函数==============================

如果roger.c不做任何修改,会有一个自带的函数confirm_roger_compiled,输出的结果就是上面看到的,下面自定义一个函数。
函数名:roger_test($str)
功能:返回 “your input string:”.$str;

 

重复上面的步骤,修改完config.m4,接着修改php_roger.h和roger.c;
vim php_roger.h
找到:PHP_FUNCTION(confirm_roger_compiled); ,新增一行:
PHP_FUNCTION(roger_test);

 

 

Php代码   收藏代码
  1. PHP_FUNCTION(confirm_roger_compiled);   /* For testing, remove later. */  
  2. PHP_FUNCTION(roger_test);       /* For testing, remove later. */  
 

保存退出。

vim roger.c
数组里增加我们的函数,找到 const zend_function_entry roger_functions[] ,增加:
PHP_FE(roger_test, NULL)

 

Php代码   收藏代码
  1. const zend_function_entry roger_functions[] = {  
  2.         PHP_FE(confirm_roger_compiled, NULL) /* For testing, remove later. */  
  3.         PHP_FE(roger_test, NULL)  /* For testing, remove later. */  
  4.         {NULL, NULL, NULL} /* Must be the last line in roger_functions[] */  
  5. };  
 

 

保存退出。

再到 roger.c 文件最后面增加如下代码:

C代码   收藏代码
  1. PHP_FUNCTION(roger_test)  
  2. {  
  3.     char *arg = NULL;  
  4.     int arg_len, len;  
  5.     char *strg;  
  6.    
  7.     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {  
  8.             return;  
  9.     }  
  10.    
  11.     len = spprintf(&strg, 0, "your input string: %s\n", arg);  
  12.     RETURN_STRINGL(strg, len, 0);  
  13. }  
 

 

继续执行如下命令

 

Php代码   收藏代码
  1. ./configure --with-php-config=/usr/bin/php-config  
  2. make  
  3. make install  

 重启apache或者nginx

 

在PHP脚本里面直接调用roger_test(”hello kitty! “),结果:


Ubuntu环境下使用C语言编写Kittler阈值分割算法的步骤如下: 1. 安装OpenCV库 在终端中输入以下命令: ``` sudo apt-get install libopencv-dev ``` 2. 创建工程 在终端中创建一个新目录,并在该目录下创建一个名为`kittler_thresholding.c`的C文件: ``` mkdir kittler_thresholding cd kittler_thresholding touch kittler_thresholding.c ``` 3. 编写代码 在`kittler_thresholding.c`文件中编写以下代码: ```c #include <stdio.h> #include <stdlib.h> #include <opencv2/opencv.hpp> using namespace cv; int main(int argc, char** argv) { if (argc != 2) { printf("Usage: %s <image_path>\n", argv[0]); return -1; } // 读取灰度图像 Mat img = imread(argv[1], IMREAD_GRAYSCALE); // 计算直方图 int histSize = 256; float range[] = { 0, 256 }; const float* histRange = { range }; Mat hist; calcHist(&img, 1, 0, Mat(), hist, 1, &histSize, &histRange, true, false); // 初始化最小方差为正无穷 double min_var = std::numeric_limits<double>::infinity(); int threshold = 0; // 遍历所有阈值,计算方差 for (int t = 0; t < 256; t++) { // 计算类别1的权重与均值 double w1 = cv::sum(hist.rowRange(0, t+1))[0]; if (w1 == 0) continue; double mu1 = cv::sum(hist.rowRange(0, t+1).mul(cv::Mat::ones(1, t+1, CV_32F) * cv::Mat_<float>(range)))[0] / w1; // 计算类别2的权重与均值 double w2 = cv::sum(hist.rowRange(t+1, histSize))[0]; if (w2 == 0) continue; double mu2 = cv::sum(hist.rowRange(t+1, histSize).mul(cv::Mat::ones(1, histSize-t-1, CV_32F) * cv::Mat_<float>(range+t+1)))[0] / w2; // 计算类别1和类别2的方差之和 double var = w1 * w2 * pow(mu1 - mu2, 2); // 更新最小方差和阈值 if (var < min_var) { min_var = var; threshold = t; } } // 二值化图像 Mat binary; threshold(img, binary, threshold, 255, THRESH_BINARY); // 显示结果 namedWindow("Original Image", WINDOW_AUTOSIZE); imshow("Original Image", img); namedWindow("Binary Image", WINDOW_AUTOSIZE); imshow("Binary Image", binary); waitKey(0); return 0; } ``` 4. 编译和运行 在终端中输入以下命令编译程序: ``` g++ -o kittler_thresholding kittler_thresholding.c `pkg-config --cflags --libs opencv` ``` 然后在终端中运行以下命令: ``` ./kittler_thresholding image.jpg ``` 程序会读取`image.jpg`图像并进行Kittler阈值分割,将结果显示出来。 以上是在Ubuntu环境下使用C语言编写Kittler阈值分割算法的步骤,希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值