Python调用C++动态库,实现图像拼接(调用输出结果有问题)

#include 
#include 
#include 
#include "opencv2/opencv_modules.hpp"
#include 
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/stitching/detail/autocalib.hpp"
#include "opencv2/stitching/detail/blenders.hpp"
#include "opencv2/stitching/detail/timelapsers.hpp"
#include "opencv2/stitching/detail/camera.hpp"
#include "opencv2/stitching/detail/exposure_compensate.hpp"
#include "opencv2/stitching/detail/matchers.hpp"
#include "opencv2/stitching/detail/motion_estimators.hpp"
#include "opencv2/stitching/detail/seam_finders.hpp"
#include "opencv2/stitching/detail/warpers.hpp"
#include "opencv2/stitching/warpers.hpp"
#ifdef HAVE_OPENCV_XFEATURES2D
#include "opencv2/xfeatures2d/nonfree.hpp"
#endif
#define ENABLE_LOG 1
#define LOG(msg) std::cout << msg
#define LOGLN(msg) std::cout << msg << std::endl
using namespace std;
using namespace cv;
using namespace cv::detail;
// Default command line args

#if 1
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif


extern "C" { //由于编译过程的原因,python一般只支持c的接口
	typedef struct ImageBase {
		int w;                   //图像的宽
		int h;                    //图像的高
		int c;                    //通道数
		unsigned char *data;    //我们要写python和c++交互的数据结构,0-255的单字符指针
	}ImageMeta;
	//typedef ImageBase ImageMeta;

	DLL_API int Stitch(ImageMeta *im1, ImageMeta *im2);//函数导出,要改

};

//vector img_names;
int num_images;
bool preview = false;
bool try_cuda = false;
double work_megapix = 0.6;
double seam_megapix = 0.1;
double compose_megapix = -1;
float conf_thresh = 1.f;
#ifdef HAVE_OPENCV_XFEATURES2D
string features_type = "surf";
#else
string features_type = "orb";
#endif
string matcher_type = "homography";
string estimator_type = "homography";
string ba_cost_func = "ray";
string ba_refine_mask = "xxxxx";
bool do_wave_correct = true;
WaveCorrectKind wave_correct = detail::WAVE_CORRECT_HORIZ;
bool save_graph = false;
std::string save_graph_to;
string warp_type = "spherical";
int expos_comp_type = ExposureCompensator::GAIN_BLOCKS;
int expos_comp_nr_feeds = 1;
int expos_comp_nr_filtering = 2;
int expos_comp_block_size = 32;
float match_conf = 0.3f;
string seam_find_type = "gc_color";
int blend_type = Blender::MULTI_BAND;
int timelapse_type = Timelapser::AS_IS;//延时摄影
float blend_strength = 5;
string result_name = "D:/result.jpg";//
bool timelapse = false;//首先定义timelapse的默认布尔类型为False
int range_width = -1;

DLL_API int Stitch(ImageMeta *im1, ImageMeta *im2)//入参两个数组指针,出参一个数组指针
//vector img_list
{//一个int数;一个图片类型的列表
//predict先判断长度 然后长度作为一个参数传给
	//preview = true;
	//try_cuda = true;
	//preview = true;
	//result = 'D:/result.jpg';
	//work_megapix = -1;
	//features_type = "orb";

	Mat img1 = Mat::zeros(Size(im1->w, im1->h), CV_8UC3);
	//先从输入的指针对象提取w,h,data;将python传来的参数转变成C处理的格式。用的是相同的结构:结构体。
	img1.data = im1->data;

	Mat img2 = Mat::zeros(Size(im2->w, im2->h), CV_8UC3);
	//先从输入的指针对象提取w,h,data;将python传来的参数转变成C处理的格式。用的是相同的结构:结构体。
	img2.data = im2->data;


	//Mat img1, img2;
	//img1 = imread("D:/1Hill.jpg");
	//img2 = imread("D:/2Hill.jpg");
	vector ALLimages(2);
	ALLimages[0] = img1.clone();
	ALLimages[1] = img2.clone();
	//img_names.push_back("D:/1Hill.jpg");
	//img_names.push_back("D:/2Hill.jpg");//??
	//img_names.push_back("D:/3Hill.jpg");//??
	num_images = 2;
#if ENABLE_LOG
	int64 app_start_time = getTickCount();
#endif
#if 0
	cv::setBreakOnError(true);
#endif
	//int retval = parseCmdArgs(argc, argv);
	//if (retval)
		//return retval;
	// Check if have enough images
	//int num_images = static_cast(img_names.size());
	if (num_images < 2)
	{
		LOGLN("Need more images");
		return -1;
	}
	double work_scale = 1, seam_scale = 1, compose_scale = 1;
	bool is_work_scale_set = false, is_seam_scale_set = false, is_compose_scale_set = false;
	LOGLN("Finding features...");
#if ENABLE_LOG
	int64 t = getTickCount();
#endif
	Ptr finder;
	if (features_type == "orb")
	{
		finder = ORB::create();
	}
	else if (features_type == "akaze")
	{
		finder = AKAZE::create();
	}
#ifdef HAVE_OPENCV_XFEATURES2D
	else if (features_type == "surf")
	{
		finder = xfeatures2d::SURF::create();
	}
	else if (features_type == "sift") {
		finder = xfeatures2d::SIFT::create();
	}
#endif
	else
	{
		cout << "Unknown 2D features type: '" << features_type << "'.\n";
		return -1;
	}
	Mat full_img, img;
	vector features(num_images);
	vector images(num_images);
	vector full_img_sizes(num_images);
	double seam_work_aspect = 1;
	for (int i = 0; i < num_images; ++i)
	{
		full_img = ALLimages[i];

		//获取一张图。imread_img -------->Mat
		//先把一边调通了再去组合调试,分治
		//full_img = img_list;
		//python传进来n张图片的base64,可以转成读取后的图片。

		//先在c中定义图像HWC结构数组数组转一次 Mat, dll返回Mat结果,Mat转一次结构体
		//main输入 Mat1,Mat2
		//dll返回数组,python转化成cv2image,然后输出image2base64

		//full_image里面是读取的imread_img类型
		//base64的size容易确定
		//先在predict前提取到图片的整个Mat传给DLL
		full_img_sizes[i] = full_img.size();//结果:full_img_sizes = [(500,300),(200,100)]
		if (full_img.empty())
		{
			//LOGLN("Can't open image " << img_names[i]);//访问了空指针,和img_names有关
			return -2;
		}
		if (work_megapix < 0)
		{
			img = full_img;
			work_scale = 1;
			is_work_scale_set = true;
		}
		else
		{
			if (!is_work_scale_set)
			{
				work_scale = min(1.0, sqrt(work_megapix * 1e6 / full_img.size().area()));
				is_work_scale_set = true;
			}
			resize(full_img, img, Size(), work_scale, work_scale, INTER_LINEAR_EXACT);
		}
		if (!is_seam_scale_set)
		{
			seam_scale = min(1.0, sqrt(seam_megapix * 1e6 / full_img.size().area()));
			seam_work_aspect = seam_scale / work_scale;
			is_seam_scale_set = true;
		}
		computeImageFeatures(finder, img, features[i]);
		features[i].img_idx = i;
		LOGLN("Features in image #" << i + 1 << ": " << features[i].keypoints.size());
		resize(full_img, img, Size(), seam_scale, seam_scale, INTER_LINEAR_EXACT);
		images[i] = img.clone();
		//循环是为了找到每张图的特征,然后把图片copy到images里
	}
	full_img.release();
	img.release();
	LOGLN("Finding features, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
	LOG("Pairwise matching");
#if ENABLE_LOG
	t = getTickCount();
#endif
	vector pairwise_matches;
	Ptr matcher;
	if (matcher_type == "affine")
		matcher = makePtr(false, try_cuda, match_conf);
	else if (range_width == -1)
		matcher = makePtr(try_cuda, match_conf);
	else
		matcher = makePtr(range_width, try_cuda, match_conf);
	(*matcher)(features, pairwise_matches);
	matcher->collectGarbage();
	LOGLN("Pairwise matching, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
	// Check if we should save matches graph
	//if (save_graph)
	//{
	//	LOGLN("Saving matches graph...");
	//	ofstream f(save_graph_to.c_str());
	//	f << matchesGraphAsString(img_names, pairwise_matches, conf_thresh);
	//}
	// Leave only images we are sure are from the same panorama
	vector indices = leaveBiggestComponent(features, pairwise_matches, conf_thresh);
	if (indices.size() != 2)//判断两个图片的相关性
		return -1;

	if (num_images < 2)
	{
		LOGLN("Need more images");
		return -1;
	}
	Ptr estimator;
	if (estimator_type == "affine")
		estimator = makePtr();
	else
		estimator = makePtr();
	vector cameras;
	if (!(*estimator)(features, pairwise_matches, cameras))
	{
		cout << "Homography estimation failed.\n";
		return -1;
	}
	for (size_t i = 0; i < cameras.size(); ++i)
	{
		Mat R;
		cameras[i].R.convertTo(R, CV_32F);
		cameras[i].R = R;
		//LOGLN("Initial camera intrinsics #" << indices[i] + 1 << ":\nK:\n" << cameras[i].K() << "\nR:\n" << cameras[i].R);
	}
	Ptr adjuster;
	if (ba_cost_func == "reproj") adjuster = makePtr();
	else if (ba_cost_func == "ray") adjuster = makePtr();
	else if (ba_cost_func == "affine") adjuster = makePtr();
	else if (ba_cost_func == "no") adjuster = makePtr();
	else
	{
		cout << "Unknown bundle adjustment cost function: '" << ba_cost_func << "'.\n";
		return -1;
	}
	adjuster->setConfThresh(conf_thresh);
	Mat_ refine_mask = Mat::zeros(3, 3, CV_8U);
	if (ba_refine_mask[0] == 'x') refine_mask(0, 0) = 1;
	if (ba_refine_mask[1] == 'x') refine_mask(0, 1) = 1;
	if (ba_refine_mask[2] == 'x') refine_mask(0, 2) = 1;
	if (ba_refine_mask[3] == 'x') refine_mask(1, 1) = 1;
	if (ba_refine_mask[4] == 'x') refine_mask(1, 2) = 1;
	adjuster->setRefinementMask(refine_mask);
	if (!(*adjuster)(features, pairwise_matches, cameras))
	{
		cout << "Camera parameters adjusting failed.\n";
		return -1;
	}
	// Find median focal length
	vector focals;
	for (size_t i = 0; i < cameras.size(); ++i)
	{
		//LOGLN("Camera #" << indices[i] + 1 << ":\nK:\n" << cameras[i].K() << "\nR:\n" << cameras[i].R);
		focals.push_back(cameras[i].focal);
	}
	sort(focals.begin(), focals.end());
	float warped_image_scale;
	if (focals.size() % 2 == 1)
		warped_image_scale = static_cast(focals[focals.size() / 2]);
	else
		warped_image_scale = static_cast(focals[focals.size() / 2 - 1] + focals[focals.size() / 2]) * 0.5f;
	if (do_wave_correct)
	{
		vector rmats;
		for (size_t i = 0; i < cameras.size(); ++i)
			rmats.push_back(cameras[i].R.clone());
		waveCorrect(rmats, wave_correct);
		for (size_t i = 0; i < cameras.size(); ++i)
			cameras[i].R = rmats[i];
	}
	LOGLN("Warping images (auxiliary)... ");
#if ENABLE_LOG
	t = getTickCount();
#endif
	vector corners(num_images);
	vector masks_warped(num_images);
	vector images_warped(num_images);
	vector sizes(num_images);
	vector masks(num_images);
	// Prepare images masks
	for (int i = 0; i < num_images; ++i)
	{
		masks[i].create(images[i].size(), CV_8U);
		masks[i].setTo(Scalar::all(255));
	}
	// Warp images and their masks
	Ptr warper_creator;
#ifdef HAVE_OPENCV_CUDAWARPING
	if (try_cuda && cuda::getCudaEnabledDeviceCount() > 0)
	{
		if (warp_type == "plane")
			warper_creator = makePtr();
		else if (warp_type == "cylindrical")
			warper_creator = makePtr();
		else if (warp_type == "spherical")
			warper_creator = makePtr();
	}
	else
#endif
	{
		if (warp_type == "plane")
			warper_creator = makePtr();
		else if (warp_type == "affine")
			warper_creator = makePtr();
		else if (warp_type == "cylindrical")
			warper_creator = makePtr();
		else if (warp_type == "spherical")
			warper_creator = makePtr();
		else if (warp_type == "fisheye")
			warper_creator = makePtr();
		else if (warp_type == "stereographic")
			warper_creator = makePtr();
		else if (warp_type == "compressedPlaneA2B1")
			warper_creator = makePtr(2.0f, 1.0f);
		else if (warp_type == "compressedPlaneA1.5B1")
			warper_creator = makePtr(1.5f, 1.0f);
		else if (warp_type == "compressedPlanePortraitA2B1")
			warper_creator = makePtr(2.0f, 1.0f);
		else if (warp_type == "compressedPlanePortraitA1.5B1")
			warper_creator = makePtr(1.5f, 1.0f);
		else if (warp_type == "paniniA2B1")
			warper_creator = makePtr(2.0f, 1.0f);
		else if (warp_type == "paniniA1.5B1")
			warper_creator = makePtr(1.5f, 1.0f);
		else if (warp_type == "paniniPortraitA2B1")
			warper_creator = makePtr(2.0f, 1.0f);
		else if (warp_type == "paniniPortraitA1.5B1")
			warper_creator = makePtr(1.5f, 1.0f);
		else if (warp_type == "mercator")
			warper_creator = makePtr();
		else if (warp_type == "transverseMercator")
			warper_creator = makePtr();
	}
	if (!warper_creator)
	{
		cout << "Can't create the following warper '" << warp_type << "'\n";
		return 1;
	}
	Ptr warper = warper_creator->create(static_cast(warped_image_scale * seam_work_aspect));
	for (int i = 0; i < num_images; ++i)
	{
		Mat_ K;
		cameras[i].K().convertTo(K, CV_32F);
		float swa = (float)seam_work_aspect;
		K(0, 0) *= swa; K(0, 2) *= swa;
		K(1, 1) *= swa; K(1, 2) *= swa;
		corners[i] = warper->warp(images[i], K, cameras[i].R, INTER_LINEAR, BORDER_REFLECT, images_warped[i]);
		sizes[i] = images_warped[i].size();
		warper->warp(masks[i], K, cameras[i].R, INTER_NEAREST, BORDER_CONSTANT, masks_warped[i]);
	}
	vector images_warped_f(num_images);
	for (int i = 0; i < num_images; ++i)
		images_warped[i].convertTo(images_warped_f[i], CV_32F);
	LOGLN("Warping images, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
	LOGLN("Compensating exposure...");
#if ENABLE_LOG
	t = getTickCount();
#endif
	Ptr compensator = ExposureCompensator::createDefault(expos_comp_type);
	if (dynamic_cast(compensator.get()))
	{
		GainCompensator* gcompensator = dynamic_cast(compensator.get());
		gcompensator->setNrFeeds(expos_comp_nr_feeds);
	}
	if (dynamic_cast(compensator.get()))
	{
		ChannelsCompensator* ccompensator = dynamic_cast(compensator.get());
		ccompensator->setNrFeeds(expos_comp_nr_feeds);
	}
	if (dynamic_cast(compensator.get()))
	{
		BlocksCompensator* bcompensator = dynamic_cast(compensator.get());
		bcompensator->setNrFeeds(expos_comp_nr_feeds);
		bcompensator->setNrGainsFilteringIterations(expos_comp_nr_filtering);
		bcompensator->setBlockSize(expos_comp_block_size, expos_comp_block_size);
	}
	compensator->feed(corners, images_warped, masks_warped);
	LOGLN("Compensating exposure, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
	LOGLN("Finding seams...");
#if ENABLE_LOG
	t = getTickCount();
#endif
	Ptr seam_finder;
	if (seam_find_type == "no")
		seam_finder = makePtr();
	else if (seam_find_type == "voronoi")
		seam_finder = makePtr();
	else if (seam_find_type == "gc_color")
	{
#ifdef HAVE_OPENCV_CUDALEGACY
		if (try_cuda && cuda::getCudaEnabledDeviceCount() > 0)
			seam_finder = makePtr(GraphCutSeamFinderBase::COST_COLOR);
		else
#endif
			seam_finder = makePtr(GraphCutSeamFinderBase::COST_COLOR);
	}
	else if (seam_find_type == "gc_colorgrad")
	{
#ifdef HAVE_OPENCV_CUDALEGACY
		if (try_cuda && cuda::getCudaEnabledDeviceCount() > 0)
			seam_finder = makePtr(GraphCutSeamFinderBase::COST_COLOR_GRAD);
		else
#endif
			seam_finder = makePtr(GraphCutSeamFinderBase::COST_COLOR_GRAD);
	}
	else if (seam_find_type == "dp_color")
		seam_finder = makePtr(DpSeamFinder::COLOR);
	else if (seam_find_type == "dp_colorgrad")
		seam_finder = makePtr(DpSeamFinder::COLOR_GRAD);
	if (!seam_finder)
	{
		cout << "Can't create the following seam finder '" << seam_find_type << "'\n";
		return 1;
	}
	seam_finder->find(images_warped_f, corners, masks_warped);
	LOGLN("Finding seams, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
	// Release unused memory
	images.clear();
	images_warped.clear();
	images_warped_f.clear();
	masks.clear();
	LOGLN("Compositing...");
#if ENABLE_LOG
	t = getTickCount();
#endif
	Mat img_warped, img_warped_s;
	Mat dilated_mask, seam_mask, mask, mask_warped;
	Ptr blender;
	Ptr timelapser;
	//double compose_seam_aspect = 1;
	double compose_work_aspect = 1;
	for (int img_idx = 0; img_idx < num_images; ++img_idx)
	{
		//LOGLN("Compositing image #" << indices[img_idx] + 1);
		// Read image and resize it if necessary
		full_img = ALLimages[img_idx];
		if (!is_compose_scale_set)
		{
			if (compose_megapix > 0)
				compose_scale = min(1.0, sqrt(compose_megapix * 1e6 / full_img.size().area()));
			is_compose_scale_set = true;
			// Compute relative scales
			//compose_seam_aspect = compose_scale / seam_scale;
			compose_work_aspect = compose_scale / work_scale;
			// Update warped image scale
			warped_image_scale *= static_cast(compose_work_aspect);
			warper = warper_creator->create(warped_image_scale);
			// Update corners and sizes
			for (int i = 0; i < num_images; ++i)
			{
				// Update intrinsics
				cameras[i].focal *= compose_work_aspect;
				cameras[i].ppx *= compose_work_aspect;
				cameras[i].ppy *= compose_work_aspect;
				// Update corner and size
				Size sz = full_img_sizes[i];
				Mat K;
				cameras[i].K().convertTo(K, CV_32F);
				Rect roi = warper->warpRoi(sz, K, cameras[i].R);
				corners[i] = roi.tl();
				sizes[i] = roi.size();
			}
		}
		if (abs(compose_scale - 1) > 1e-1)//没用
			resize(full_img, img, Size(), compose_scale, compose_scale, INTER_LINEAR_EXACT);
		else
			img = full_img;
		full_img.release();
		Size img_size = img.size();
		Mat K;
		cameras[img_idx].K().convertTo(K, CV_32F);
		// Warp the current image
		warper->warp(img, K, cameras[img_idx].R, INTER_LINEAR, BORDER_REFLECT, img_warped);
		// Warp the current image mask
		mask.create(img_size, CV_8U);
		mask.setTo(Scalar::all(255));
		warper->warp(mask, K, cameras[img_idx].R, INTER_NEAREST, BORDER_CONSTANT, mask_warped);
		// Compensate exposure
		compensator->apply(img_idx, corners[img_idx], img_warped, mask_warped);
		img_warped.convertTo(img_warped_s, CV_16S);
		img_warped.release();
		img.release();
		mask.release();
		dilate(masks_warped[img_idx], dilated_mask, Mat());
		resize(dilated_mask, seam_mask, mask_warped.size(), 0, 0, INTER_LINEAR_EXACT);
		mask_warped = seam_mask & mask_warped;
		if (!blender && !timelapse)//blender是False,timelapse也是False,这里运行了!
		{//做multiband
			blender = Blender::createDefault(blend_type, try_cuda);
			Size dst_sz = resultRoi(corners, sizes).size();
			float blend_width = sqrt(static_cast(dst_sz.area())) * blend_strength / 100.f;
			if (blend_width < 1.f)
				blender = Blender::createDefault(Blender::NO, try_cuda);
			else if (blend_type == Blender::MULTI_BAND)
			{
				MultiBandBlender* mb = dynamic_cast(blender.get());
				mb->setNumBands(static_cast(ceil(log(blend_width) / log(2.)) - 1.));
				LOGLN("Multi-band blender, number of bands: " << mb->numBands());
			}
			else if (blend_type == Blender::FEATHER)//未运行
			{
				FeatherBlender* fb = dynamic_cast(blender.get());
				fb->setSharpness(1.f / blend_width);
				LOGLN("Feather blender, sharpness: " << fb->sharpness());
			}
			blender->prepare(corners, sizes);
		}
		else if (!timelapser && timelapse)//timelapse是假,timelapser是什么??没运行
		{
			timelapser = Timelapser::createDefault(timelapse_type);
			timelapser->initialize(corners, sizes);
			cout << "----------------------------运行---------------------------------" << endl;
		}
		// Blend the current image
		if (timelapse)//默认是假
		{
			cout << "----------------------------运行2---------------------------------" << endl;
		}
		else
		{//这里运行了两次,因为在循环体中,图片有两张
			blender->feed(img_warped_s, mask_warped, corners[img_idx]);
			cout << "----------------------------运行3---------------------------------" << endl;
		}
	}
	if (!timelapse)//运行了
	{
		Mat result, result_mask;
		blender->blend(result, result_mask);
		LOGLN("Compositing, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec");
		imwrite(result_name, result);
		//(result.cols)*(result.rows)
		memcpy(im2->data, result.clone().data, (result.cols)*(result.rows));//从哪里拷贝多少个字节。。
		im2->w = result.cols;
		im2->h = result.rows;
		im2->c = 3;
	}
	LOGLN("Finished, total time: " << ((getTickCount() - app_start_time) / getTickFrequency()) << " sec");
	return 0;
}
from ctypes import *
from io import BytesIO
import numpy as np
import cv2


# 写法是yolo的darknet.py里看到的,学以致用

def c_array(ctype, values):  # 把图像的数据转化为内存连续的 列表 , 使c++能使用这块内存
    arr = (ctype * len(values))()
    arr[:] = values
    return arr


def array_to_image(arr):
    c = arr.shape[2]
    h = arr.shape[0]
    w = arr.shape[1]
    arr = arr.flatten()#转化成图片后成了一维的
    data = c_array(c_uint8, arr)
    im = IMAGE(w, h, c, data)#将读进来数组转化成c接受的形式,调用class IMAGE
    return im


class IMAGE(Structure):  # 这里和ImgSegmentation.hpp里面的结构体保持一致。
    _fields_ = [("w", c_int),
                ("h", c_int),
                ("c", c_int),
                ("data", POINTER(c_uint8))]


img1 = cv2.imread('D:/1Hill.jpg')
img2 = cv2.imread('D:/2Hill.jpg')
#h, w, c = img.shape[0], img.shape[1], img.shape[2]
#h, w, c = img.shape[0], img.shape[1], img.shape[2]

#gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) * 0
#gray = np.reshape(gray, (h, w, 1))  # 一定要使用(h, w, 1),最后的1别忘。
im1 = array_to_image(img1)#这里是将读进来的cv_imread格式图片转化成结构体,一维的
im2 = array_to_image(img2)
#gray_img = array_to_image(gray)

lib = cdll.LoadLibrary('./image_stiching.dll')  # 读取动态库文件
lib.Stitch.argtypes = [POINTER(IMAGE), POINTER(IMAGE)]  # 设置函数入参格式,声明采用指针传递。指定入参为2个数组指针,python里定义类型指针和C相反,类型在后。
#lib.Stitch.restype = c_int64
lib.Stitch(im1, im2)  # 执行函数,这里直接修改gray_img的内存数据。入参是非指针,python提取地址作为输入。因为函数原型是传递的指针,这里相当于POINTER会自动取输入im1,im2的指针作为入参。
## 因此输入的内存数据会直接被改变。
y = im2.data  # 获取data,被改变传递改变的对象名
array_length = im2.h * im2.w
#转化为numpy的ndarray
buffer_from_memory = pythonapi.PyMemoryView_FromMemory  # 这个是python 3的使用方法,提取运算缓存
buffer_from_memory.restype = py_object                  #提取缓存返回的数据格式,以上两步是下一步从缓存中提取某个变量的结果必须的。
buffer = buffer_from_memory(y, array_length)                    #提取底层的缓存指针,指定提取缓存大小
img = np.frombuffer(buffer, dtype=np.uint8)                         #提取到缓存中的数组
print("----------------------")
print(img.shape)
img = np.reshape(img, (im2.h, im2.w,1))                   #改变缓存数组的格式,用于显示
print("-------2---------")
print(img.shape)
print(img)
cv2.imshow('test', img)
cv2.imwrite("D:/RESULT_PY.JPG",img)
cv2.waitKey(0)

 

你可能感兴趣的:(图像拼接)