脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python 调用 C++ 传递numpy 数据详情

Python 调用 C++ 传递numpy 数据详情

2022-11-16 13:19darkerJ Python

这篇文章主要介绍了Python 调用 C++ 传递numpy 数据详情,文章主要分为两部分,c++代码和python代码,代码分享详细,需要的小伙伴可以参考一下,希望对你有所帮助

1.C++ 代码

Demo.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#pragma once
void GeneratorGaussKernel(int ksize, float sigma, float* kernel);
 
void LeftAndRightMirrorImageUInt8(unsigned char* in, unsigned char* out, int width, int height);
 
void LeftAndRightMirrorImageFloat(float* in, float* out, int width, int height);
 
void UpAndDownMirrorImageFloat(float* in, float* out, int width, int height);
 
void UpAndDownMirrorImageUInt8(unsigned char* in, unsigned char* out, int width, int height);
void ImageFilterFloat(float* in, float* out, int width, int height, float* filterKernel, int kw, int kh);
 
void SaltAndPepperFloat(float* in, float* out, int width, int height, float minV, float maxV, float proportion);
 
void SaltAndPepperUInt8(unsigned char* in, unsigned char* out, int width, int height, float minV, float maxV, float proportion);
void ImageMinMax(float* in, int width, int height, int channels, float* minV, float* maxV);
 
void ImageMinMax(unsigned char* in, int width, int height, int channels, unsigned char* minV, unsigned char* maxV);
void ImageMulAAddBFloatFloat(float* in, float* out, int width, int height, int channels, float A, float B);
 
void ImageMulAAddBUInt8UInt8(unsigned char* in, unsigned char* out, int width, int height, int channels, float A, float B);
void ImageMulAAddBUInt8Float(unsigned char* in, float* out, int width, int height, int channels, float A, float B);
 
void NormalizeUInt8Float(unsigned char* in, float* out, int width, int height, int channels, int type);
void NormalizeFloatFloat(float* in, float* out, int width, int height, int channels, int type);
void RGBAvgUInt8Float(unsigned char* in, float* out, int width, int height);
void RGBAvgFloatFloat(float* in, float* out, int width, int height);

Demo.cpp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
#include <Python.h>
#include <malloc.h>
#include <numpy/arrayobject.h>
#include <iostream>
#include <vector>
#include <xmmintrin.h>
#include <immintrin.h>
#include "omp.h"
 
class ImageCoord {
public:
    ImageCoord() {
        x = 0; y = 0;
    }
 
    ImageCoord(const ImageCoord& coord) {
        x = coord.x;
        y = coord.y;
    }
    ImageCoord(int x, int y) {
        this->x = x;
        this->y = y;
    }
    void operator= (ImageCoord& coord) {
        x = coord.x;
        y = coord.y;
    }
 
    int x, y;
};
 
class Random {
public:
    Random() {
        srand((unsigned int)time(NULL));
    }
 
    ImageCoord RandomImageCoord(int width, int height) {
        ImageCoord ans;
        ans.x = rand() % width;
        ans.y = rand() % height;
        return ans;
    }
    bool RandomBoolean() {
        return rand() % 2 == 1;
    }
};
 
static Random gRandom;
 
void GeneratorGaussKernel(int ksize, float sigma, float* kernel)
{
    int bufferSize = ksize * ksize;
    float sigmasigma2 = 2.0f * sigma * sigma;
    float sigmasigma2Inv = 1.f / sigmasigma2;
    float sigmasigma2PIInv = sigmasigma2Inv / 3.14159265358979f;
    int radius = ksize / 2;
    float sum = 0.f;
    for (int i = -radius; i <= radius; ++i) {
        for (int j = -radius; j <= radius; ++j) {
            kernel[(i + radius) * ksize + (j + radius)] = sigmasigma2PIInv * expf(-(i * i + j * j) * sigmasigma2Inv);
        }
    }
 
    for (int i = 0; i < bufferSize; ++i) {
        sum += kernel[i];
    }
    sum = 1.f / sum;
    for (int i = 0; i < bufferSize; ++i) {
        kernel[i] = kernel[i] * sum;
    }
}
 
void LeftAndRightMirrorImageUInt8(unsigned char* in, unsigned char* out, int width, int height)
{
    for (int i = 0; i < height; ++i) {
        int hoffset = i * width;
        for (int j = 0; j < width; ++j) {
 
            int woffset = (hoffset + j) * 3;
            int woffset_ = (hoffset + width - 1 - j) * 3;
            for (int n = 0; n < 3; ++n) {
                out[woffset_ + n] = in[woffset + n];
            }
        }
    }
}
 
void LeftAndRightMirrorImageFloat(float* in, float* out, int width, int height)
{
    for (int i = 0; i < height; ++i) {
        int hoffset = i * width;
        for (int j = 0; j < width; ++j) {
            int woffset = (hoffset + j) * 3;
            int woffset_ = (hoffset + width - 1 - j) * 3;
            for (int n = 0; n < 3; ++n) {
                out[woffset_ + n] = in[woffset + n];
            }
        }
    }
}
 
void UpAndDownMirrorImageFloat(float* in, float* out, int width, int height)
{
    int lineOffset = width * 3;
    int lineSize = lineOffset * sizeof(float);
    float* outTmp = out + lineOffset * height - lineOffset;
    float* inTmp = in;
    for (int i = 0; i < height; ++i) {
        memcpy_s(outTmp, lineSize, inTmp, lineSize);
        outTmp -= lineOffset;
        inTmp += lineOffset;
    }
}
 
void UpAndDownMirrorImageUInt8(unsigned char* in, unsigned char* out, int width, int height)
{
    int lineOffset = width * 3;
    int lineSize = lineOffset * sizeof(unsigned char);
    unsigned char* outTmp = out + lineOffset * height - lineOffset;
    unsigned char* inTmp = in;
    for (int i = 0; i < height; ++i) {
        memcpy_s(outTmp, lineSize, inTmp, lineSize);
        outTmp -= lineOffset;
        inTmp += lineOffset;
    }
}
 
#if 0
 
void Conv(float* in, float* out, int width, float* filter, int ksize) {
 
    int lineSize = width * 3;
    float* inTemp = in;
    float* outTemp = out;
    out[0] = 0.f; out[1] = 0.f; out[2] = 0.f;
    for (int i = 0; i < ksize; ++i) {
        for (int j = 0; j < ksize; ++j) {
            int xoffset = j * 3;
            out[0] += (*filter) * inTemp[xoffset + 0];
            out[1] += (*filter) * inTemp[xoffset + 1];
            out[2] += (*filter) * inTemp[xoffset + 2];
            filter++;
        }
        inTemp = inTemp + lineSize;
    }
}
 
void ImageFilterFloat(float* in, float* out, int width, int height, float* filterKernel, int kw, int kh)
{
    size_t size = (size_t)width * (size_t)height * sizeof(float) * 3;
 
    int startX = kw / 2;
    int endX = width - kw / 2;
    
    int startY = kh / 2;
    int endY = height - kh / 2;
 
    float* tempOut = out + (startY * width + startX) * 3;
 
    memset(out, 0, size);
    //memcpy_s(out, size, in, size);
    omp_set_num_threads(32);
 
#pragma omp parallel for
    for (int i = 0; i <= height - kh; ++i) {
        int yoffset = i * width * 3;
        for (int j = 0; j <= width - kw; ++j) {
            int xoffset = yoffset + j * 3;
            Conv((in + xoffset), (tempOut + xoffset), width, filterKernel, kw);
        }
 
    }
}
#elif 1
 
void Conv(float* in, float* out, int width, __m128* filter, int ksize) {
 
    int lineSize = width * 3;
    float* inTemp = in;
    float* outTemp = out;
    out[0] = 0.f; out[1] = 0.f; out[2] = 0.f;
    __m128 sum = _mm_set_ps1(0.f);
    for (int i = 0; i < ksize; ++i) {
        for (int j = 0; j < ksize; ++j) {
            int xoffset = j * 3;
 
            __m128 img_value = _mm_set_ps(1.f, inTemp[xoffset + 2], inTemp[xoffset + 1], inTemp[xoffset + 0]);
 
            sum = _mm_add_ps(_mm_mul_ps((*filter), img_value), sum);
 
            filter++;
        }
        inTemp = inTemp + lineSize;
    }
    out[0] = sum.m128_f32[0];
    out[1] = sum.m128_f32[1];
    out[2] = sum.m128_f32[2];
}
 
void ImageFilterFloat(float* in, float* out, int width, int height, float* filterKernel, int kw, int kh)
{
    size_t size = (size_t)width * (size_t)height * sizeof(float) * 3;
 
    int startX = kw / 2;
    int endX = width - kw / 2;
 
    int startY = kh / 2;
    int endY = height - kh / 2;
 
    float* tempOut = out + (startY * width + startX) * 3;
 
    memset(out, 0, size);
 
    __m128* filterKernel_m128 = (__m128*)_mm_malloc(kw * kh * sizeof(__m128), sizeof(__m128));
    for (int i = 0; i < kw * kh; ++i) {
        filterKernel_m128[i] = _mm_set_ps1(filterKernel[i]);
    }
 
 
    omp_set_num_threads(32);
#pragma omp parallel for
    for (int i = 0; i <= height - kh; ++i) {
        int yoffset = i * width * 3;
        for (int j = 0; j <= width - kw; ++j) {
            int xoffset = yoffset + j * 3;
            Conv((in + xoffset), (tempOut + xoffset), width, filterKernel_m128, kw);
        }
 
    }
 
    if (filterKernel_m128) {
        _mm_free(filterKernel_m128);
        filterKernel_m128 = NULL;
    }
}
 
#endif
void SaltAndPepperFloat(float* in, float* out, int width, int height, float minV, float maxV, float proportion)
{
    int coordNumber = (int)(width * height * proportion);
 
    if (in != out) {
        memcpy_s(out, width * height * 3 * sizeof(float), in, width * height * 3 * sizeof(float));
    }
 
    for (int i = 0; i < coordNumber; ++i) {
        ImageCoord coord = gRandom.RandomImageCoord(width, height);
        bool saltOrPepper = gRandom.RandomBoolean();
        float value = saltOrPepper ? minV : maxV;
        int x = coord.x;
        int y = coord.y;
        int offset = (y * width + x) * 3;
        for (int c = 0; c < 3; ++c) {
            out[offset + c] = value;
        }
    }
}
 
void SaltAndPepperUInt8(unsigned char* in, unsigned char* out, int width, int height, float minV, float maxV, float proportion)
{
    int coordNumber = (int)(width * height * proportion);
 
    if (in != out) {
        memcpy_s(out, width * height * 3 * sizeof(unsigned char), in, width * height * 3 * sizeof(unsigned char));
    }
    for (int i = 0; i < coordNumber; ++i) {
        ImageCoord coord = gRandom.RandomImageCoord(width, height);
        bool saltOrPepper = gRandom.RandomBoolean();
        float value = saltOrPepper ? minV : maxV;
        int x = coord.x;
        int y = coord.y;
        int offset = (y * width + x) * 3;
        for (int c = 0; c < 3; ++c) {
            out[offset + c] = (unsigned char)value;
        }
    }
}
 
void ImageMinMax(float* in, int width, int height, int channels, float* minV, float* maxV)
{
    float minValue = 99999.f;
    float maxValue = -minValue;
    int number = width * height * channels;
    for (int i = 0; i < number; ++i) {
        float value = in[i];
        if (value > maxValue) {
            maxValue = value;
        }
        if (value < minValue) {
            minValue = value;
        }
    }
    *minV = (float)minValue;
    *maxV = (float)maxValue;
}
 
void ImageMinMax(unsigned char* in, int width, int height, int channels, unsigned char* minV, unsigned char* maxV)
{
    int minValue = 256;
    int maxValue = -1;
    int number = width * height * channels;
    for (int i = 0; i < number; ++i) {
        int value = in[i];
        if (value > maxValue) {
            maxValue = value;
        }
        if (value < minValue) {
            minValue = value;
        }
    }
    *minV = (unsigned char)minValue;
    *maxV = (unsigned char)maxValue;
}
 
void ImageMulAAddBFloatFloat(float* in, float* out, int width, int height, int channels, float A, float B)
{
    int size = width * height * channels;
    for (int i = 0; i < size; ++i) {
        out[i] = in[i] * A + B;
    }
}
 
void ImageMulAAddBUInt8UInt8(unsigned char* in, unsigned char* out, int width, int height, int channels, float A, float B)
{
#define ALVACLAMP(x, minV, maxV) \
        (x) < (minV) ? (minV) : ((x) > (maxV) ? (maxV) : (x))
    int size = width * height * channels;
    for (int i = 0; i < size; ++i) {
        out[i] = (unsigned char)(ALVACLAMP(in[i] * A + B, 0, 255));
    }
#undef ALVACLAMP
}
 
void ImageMulAAddBUInt8Float(unsigned char* in, float* out, int width, int height, int channels, float A, float B)
{
    int size = width * height * channels;
    for (int i = 0; i < size; ++i) {
        out[i] = in[i] * A + B;
    }
}
 
void NormalizeUInt8Float(unsigned char* in, float* out, int width, int height, int channels, int type)
{
    unsigned char minV, maxV;
    ImageMinMax(in, width, height, channels, &minV, &maxV);
    int size = width * height * channels;
    float inv = 1.f / (maxV - minV);
    float offset = 0.f;
    if (type == 1) {
        inv *= 2.f;
        offset = -1.f;
    }
    for (int i = 0; i < size; ++i) {
        out[i] = (in[i] - minV) * inv + offset;
    }
}
 
void NormalizeFloatFloat(float* in, float* out, int width, int height, int channels, int type)
{
    float minV, maxV;
    ImageMinMax(in, width, height, channels, &minV, &maxV);
    int size = width * height * channels;
    float inv = 1.f / (maxV - minV);
    float offset = 0.f;
    if (type == 1) {
        inv *= 2.f;
        offset = -1.f;
    }
    for (int i = 0; i < size; ++i) {
        out[i] = (in[i] - minV) * inv + offset;
    }
}
 
void RGBAvgUInt8Float(unsigned char* in, float* out, int width, int height)
{
    int size = width * height;
    for (int i = 0; i < size; ++i) {
        float avg = (in[i * 3 + 0] + in[i * 3 + 1] + in[i * 3 + 2]) / 3.f;
        out[i * 3 + 0] = avg;
        out[i * 3 + 1] = avg;
        out[i * 3 + 2] = avg;
    }
}
 
void RGBAvgFloatFloat(float* in, float* out, int width, int height)
{
    int size = width * height;
    for (int i = 0; i < size; ++i) {
        float avg = (in[i * 3 + 0] + in[i * 3 + 1] + in[i * 3 + 2]) / 3.f;
        out[i * 3 + 0] = avg;
        out[i * 3 + 1] = avg;
        out[i * 3 + 2] = avg;
    }
}
 
static PyObject* GeneratorGaussKernel(PyObject* self, PyObject* args) {
    //int ksize, float sigma, float* kernel
    PyObject* pyobj_filter = NULL;
    int ksize;
    float sigma;
    int ret = PyArg_ParseTuple(args, "Oif", &pyobj_filter, &ksize, &sigma);
 
    PyArrayObject* oarr = (PyArrayObject*)pyobj_filter;
 
    float* data = (float*)(oarr->data);
 
    GeneratorGaussKernel(ksize, sigma, data);
    PyObject* result = PyUnicode_FromFormat("result:%s", "ok");
    return result;
}
 
static PyObject* LeftAndRightMirrorImageUInt8(PyObject* self, PyObject* args) {
    PyObject* pyobj_img = NULL;
    PyObject* pyobj_out_img = NULL;
    int width, height;
    int ret = PyArg_ParseTuple(args, "OOii", &pyobj_img, &pyobj_out_img, &width, &height);
    PyArrayObject* oarr = (PyArrayObject*)pyobj_img;
 
    PyArrayObject* oarr_out = (PyArrayObject*)pyobj_out_img;
 
    unsigned char* dataIn = (unsigned char*)(oarr->data);
    unsigned char* dataOut = (unsigned char*)(oarr_out->data);
    
    LeftAndRightMirrorImageUInt8(dataIn, dataOut, width, height);
 
    PyObject* result = PyUnicode_FromFormat("result:%s", "ok");
    return result;
 
    //std::cout << "";
}
 
static PyObject* LeftAndRightMirrorImageFloat(PyObject* self, PyObject* args) {
 
    PyObject* pyobj_img = NULL;
    PyObject* pyobj_out_img = NULL;
    int width, height;
    int ret = PyArg_ParseTuple(args, "OOii", &pyobj_img, &pyobj_out_img, &width, &height);
 
    PyArrayObject* oarr = (PyArrayObject*)pyobj_img;
 
    PyArrayObject* oarr_out = (PyArrayObject*)pyobj_out_img;
 
    float* dataIn = (float*)(oarr->data);    
    float* dataOut = (float*)(oarr_out->data);
 
    LeftAndRightMirrorImageFloat(dataIn, dataOut, width, height);
 
    PyObject* result = PyUnicode_FromFormat("result:%s", "ok");
    return result;
}
 
static PyObject* UpAndDownMirrorImageUInt8(PyObject* self, PyObject* args) {
 
    PyObject* pyobj_img = NULL;
    PyObject* pyobj_out_img = NULL;
    int width, height;
    int ret = PyArg_ParseTuple(args, "OOii", &pyobj_img, &pyobj_out_img, &width, &height);
 
    PyArrayObject* oarr = (PyArrayObject*)pyobj_img;
 
    PyArrayObject* oarr_out = (PyArrayObject*)pyobj_out_img;
 
    unsigned char* dataIn = (unsigned char*)(oarr->data);
 
    unsigned char* dataOut = (unsigned char*)(oarr_out->data);
 
    UpAndDownMirrorImageUInt8(dataIn, dataOut, width, height);
    PyObject* result = PyUnicode_FromFormat("result:%s", "ok");
    return result;
}
 
static PyObject* UpAndDownMirrorImageFloat(PyObject* self, PyObject* args) {
 
    PyObject* pyobj_img = NULL;
    PyObject* pyobj_out_img = NULL;
    int width, height;
    int ret = PyArg_ParseTuple(args, "OOii", &pyobj_img, &pyobj_out_img, &width, &height);
 
    PyArrayObject* oarr = (PyArrayObject*)pyobj_img;
 
    PyArrayObject* oarr_out = (PyArrayObject*)pyobj_out_img;
 
    float* dataIn = (float*)(oarr->data);
    float* dataOut = (float*)(oarr_out->data);
 
    UpAndDownMirrorImageFloat(dataIn, dataOut, width, height);
    PyObject* result = PyUnicode_FromFormat("result:%s", "ok");
    return result;
}
 
static PyObject* ImageFilterFloat(PyObject* self, PyObject* args) {
    //float* in, float* out, int width, int height, float* filterKernel, int kw, int kh
    PyObject* pyobj_img = NULL;
    PyObject* pyobj_out_img = NULL;
    PyObject* pyobj_filterKernel = NULL;
    int width, height;
    int kw, kh;
    int ret = PyArg_ParseTuple(args, "OOiiOii", &pyobj_img, &pyobj_out_img, &width, &height, &pyobj_filterKernel, &kw, &kh);
 
    PyArrayObject* oarr = (PyArrayObject*)pyobj_img;
    PyArrayObject* oarr_out = (PyArrayObject*)pyobj_out_img;
    PyArrayObject* kernel = (PyArrayObject*)pyobj_filterKernel;
 
    float* dataIn = (float*)(oarr->data);
    float* dataOut = (float*)(oarr_out->data);
    float* filter = (float*)(kernel->data);
 
    ImageFilterFloat(dataIn, dataOut, width, height, filter, kw, kh);
    PyObject* result = PyUnicode_FromFormat("result:%s", "ok");
    return result;
}
 
static PyObject* SaltAndPepperFloat(PyObject* self, PyObject* args) {
    //float* in, float* out, int width, int height, float minV, float maxV, float proportion
    PyObject* pyobj_img = NULL;
    PyObject* pyobj_out_img = NULL;
    int width, height;
    float minV, maxV, proportion;
    int ret = PyArg_ParseTuple(args, "OOiifff", &pyobj_img, &pyobj_out_img, &width, &height, &minV, &maxV, &proportion);
 
    PyArrayObject* oarr = (PyArrayObject*)pyobj_img;
    PyArrayObject* oarr_out = (PyArrayObject*)pyobj_out_img;
 
    float* dataIn = (float*)(oarr->data);
    float* dataOut = (float*)(oarr_out->data);
 
    SaltAndPepperFloat(dataIn, dataOut, width, height, minV, maxV, proportion);
    PyObject* result = PyUnicode_FromFormat("result:%s", "ok");
    return result;
}
 
static PyObject* SaltAndPepperUInt8(PyObject* self, PyObject* args) {
    //float* in, float* out, int width, int height, float minV, float maxV, float proportion
    PyObject* pyobj_img = NULL;
    PyObject* pyobj_out_img = NULL;
    int width, height;
    float minV, maxV, proportion;
    int ret = PyArg_ParseTuple(args, "OOiifff", &pyobj_img, &pyobj_out_img, &width, &height, &minV, &maxV, &proportion);
 
    PyArrayObject* oarr = (PyArrayObject*)pyobj_img;
    PyArrayObject* oarr_out = (PyArrayObject*)pyobj_out_img;
 
    unsigned char* dataIn = (unsigned char*)(oarr->data);
    unsigned char* dataOut = (unsigned char*)(oarr_out->data);
 
    SaltAndPepperUInt8(dataIn, dataOut, width, height, minV, maxV, proportion);
    PyObject* result = PyUnicode_FromFormat("result:%s", "ok");
    return result;
}
 
static PyObject* ImageMulAAddBFloatFloat(PyObject* self, PyObject* args) {
    //float* in, float* out, int width, int height, int channels, float A, float B
    PyObject* pyobj_img = NULL;
    PyObject* pyobj_out_img = NULL;
    int width, height, channels = 3;
    float A, B;
    int ret = PyArg_ParseTuple(args, "OOiiff", &pyobj_img, &pyobj_out_img, &width, &height, &A, &B);
 
    PyArrayObject* oarr = (PyArrayObject*)pyobj_img;
    PyArrayObject* oarr_out = (PyArrayObject*)pyobj_out_img;
 
    float* dataIn = (float*)(oarr->data);
    float* dataOut = (float*)(oarr_out->data);
 
    ImageMulAAddBFloatFloat(dataIn, dataOut, width, height, channels, A, B);
    PyObject* result = PyUnicode_FromFormat("result:%s", "ok");
    return result;
}
 
static PyObject* ImageMulAAddBUInt8Float(PyObject* self, PyObject* args) {
    //float* in, float* out, int width, int height, int channels, float A, float B
    PyObject* pyobj_img = NULL;
    PyObject* pyobj_out_img = NULL;
    int width, height, channels = 3;
    float A, B;
    int ret = PyArg_ParseTuple(args, "OOiiff", &pyobj_img, &pyobj_out_img, &width, &height, &A, &B);
 
    PyArrayObject* oarr = (PyArrayObject*)pyobj_img;
    PyArrayObject* oarr_out = (PyArrayObject*)pyobj_out_img;
 
    unsigned char* dataIn = (unsigned char*)(oarr->data);
    float* dataOut = (float*)(oarr_out->data);
 
    ImageMulAAddBUInt8Float(dataIn, dataOut, width, height, channels, A, B);
    PyObject* result = PyUnicode_FromFormat("result:%s", "ok");
    return result;
}
 
static PyObject* ImageMulAAddBUInt8UInt8(PyObject* self, PyObject* args) {
    //float* in, float* out, int width, int height, int channels, float A, float B
    PyObject* pyobj_img = NULL;
    PyObject* pyobj_out_img = NULL;
    int width, height, channels = 3;
    float A, B;
    int ret = PyArg_ParseTuple(args, "OOiiff", &pyobj_img, &pyobj_out_img, &width, &height, &A, &B);
 
    PyArrayObject* oarr = (PyArrayObject*)pyobj_img;
    PyArrayObject* oarr_out = (PyArrayObject*)pyobj_out_img;
 
    unsigned char* dataIn = (unsigned char*)(oarr->data);
    unsigned char* dataOut = (unsigned char*)(oarr_out->data);
 
    ImageMulAAddBUInt8UInt8(dataIn, dataOut, width, height, channels, A, B);
    PyObject* result = PyUnicode_FromFormat("result:%s", "ok");
    return result;
}
 
static PyObject* NormalizeUInt8Float(PyObject* self, PyObject* args) {
    // unsigned char* in, float* out, int width, int height, int channels, int type
    PyObject* pyobj_img = NULL;
    PyObject* pyobj_out_img = NULL;
    int width, height, channels = 3;
    int type;
    int ret = PyArg_ParseTuple(args, "OOiii", &pyobj_img, &pyobj_out_img, &width, &height, &type);
 
    PyArrayObject* oarr = (PyArrayObject*)pyobj_img;
    PyArrayObject* oarr_out = (PyArrayObject*)pyobj_out_img;
 
    unsigned char* dataIn = (unsigned char*)(oarr->data);
    float* dataOut = (float*)(oarr_out->data);
 
    NormalizeUInt8Float(dataIn, dataOut, width, height, channels, type);
    PyObject* result = PyUnicode_FromFormat("result:%s", "ok");
    return result;
}
 
static PyObject* NormalizeFloatFloat(PyObject* self, PyObject* args) {
    // unsigned char* in, float* out, int width, int height, int channels, int type
    PyObject* pyobj_img = NULL;
    PyObject* pyobj_out_img = NULL;
    int width, height, channels = 3;
    int type;
    int ret = PyArg_ParseTuple(args, "OOiii", &pyobj_img, &pyobj_out_img, &width, &height, &type);
 
    PyArrayObject* oarr = (PyArrayObject*)pyobj_img;
    PyArrayObject* oarr_out = (PyArrayObject*)pyobj_out_img;
 
    float* dataIn = (float*)(oarr->data);
    float* dataOut = (float*)(oarr_out->data);
 
    NormalizeFloatFloat(dataIn, dataOut, width, height, channels, type);
    PyObject* result = PyUnicode_FromFormat("result:%s", "ok");
    return result;
}
 
static PyObject* RGBAvgUInt8Float(PyObject* self, PyObject* args) {
    // unsigned char* in, float* out, int width, int height
    PyObject* pyobj_img = NULL;
    PyObject* pyobj_out_img = NULL;
    int width, height;
    int ret = PyArg_ParseTuple(args, "OOii", &pyobj_img, &pyobj_out_img, &width, &height);
 
    PyArrayObject* oarr = (PyArrayObject*)pyobj_img;
    PyArrayObject* oarr_out = (PyArrayObject*)pyobj_out_img;
 
    unsigned char* dataIn = (unsigned char*)(oarr->data);
    float* dataOut = (float*)(oarr_out->data);
 
    RGBAvgUInt8Float(dataIn, dataOut, width, height);
    PyObject* result = PyUnicode_FromFormat("result:%s", "ok");
    return result;
}
 
static PyObject* RGBAvgFloatFloat(PyObject* self, PyObject* args) {
    // unsigned char* in, float* out, int width, int height
    PyObject* pyobj_img = NULL;
    PyObject* pyobj_out_img = NULL;
    int width, height;
    int ret = PyArg_ParseTuple(args, "OOii", &pyobj_img, &pyobj_out_img, &width, &height);
 
    PyArrayObject* oarr = (PyArrayObject*)pyobj_img;
    PyArrayObject* oarr_out = (PyArrayObject*)pyobj_out_img;
 
    float* dataIn = (float*)(oarr->data);
    float* dataOut = (float*)(oarr_out->data);
 
    RGBAvgFloatFloat(dataIn, dataOut, width, height);
    PyObject* result = PyUnicode_FromFormat("result:%s", "ok");
    return result;
}
 
static PyMethodDef DemoMethods[] = {
    {"LeftAndRightMirrorImageUInt8", (PyCFunction)LeftAndRightMirrorImageUInt8, METH_VARARGS | METH_KEYWORDS, "I guess here is description." },
    {"LeftAndRightMirrorImageFloat", (PyCFunction)LeftAndRightMirrorImageFloat, METH_VARARGS | METH_KEYWORDS, "I guess here is description." },
    {"UpAndDownMirrorImageUInt8", (PyCFunction)UpAndDownMirrorImageUInt8, METH_VARARGS | METH_KEYWORDS, "I guess here is description." },
    {"UpAndDownMirrorImageFloat", (PyCFunction)UpAndDownMirrorImageFloat, METH_VARARGS | METH_KEYWORDS, "I guess here is description." },
    {"ImageFilterFloat", (PyCFunction)ImageFilterFloat, METH_VARARGS | METH_KEYWORDS, "I guess here is description." },
    {"SaltAndPepperFloat", (PyCFunction)SaltAndPepperFloat, METH_VARARGS | METH_KEYWORDS, "I guess here is description." },
    {"SaltAndPepperUInt8", (PyCFunction)SaltAndPepperUInt8, METH_VARARGS | METH_KEYWORDS, "I guess here is description." },
    {"ImageMulAAddBFloatFloat", (PyCFunction)ImageMulAAddBFloatFloat, METH_VARARGS | METH_KEYWORDS, "I guess here is description." },
    {"ImageMulAAddBUInt8Float", (PyCFunction)ImageMulAAddBUInt8Float, METH_VARARGS | METH_KEYWORDS, "I guess here is description." },
    {"ImageMulAAddBUInt8UInt8", (PyCFunction)ImageMulAAddBUInt8UInt8, METH_VARARGS | METH_KEYWORDS, "I guess here is description." },
    {"NormalizeUInt8Float", (PyCFunction)NormalizeUInt8Float, METH_VARARGS | METH_KEYWORDS, "I guess here is description." },
    {"NormalizeFloatFloat", (PyCFunction)NormalizeFloatFloat, METH_VARARGS | METH_KEYWORDS, "I guess here is description." },
    {"RGBAvgUInt8Float", (PyCFunction)RGBAvgUInt8Float, METH_VARARGS | METH_KEYWORDS, "I guess here is description." },
    {"RGBAvgFloatFloat", (PyCFunction)RGBAvgFloatFloat, METH_VARARGS | METH_KEYWORDS, "I guess here is description." },
    {"GeneratorGaussKernel", (PyCFunction)GeneratorGaussKernel, METH_VARARGS | METH_KEYWORDS, "I guess here is description." },
    {NULL, NULL, 0, NULL}
};
 
static struct PyModuleDef demoModule = {
    PyModuleDef_HEAD_INIT,
    "mirror",
    NULL,
    -1,
    DemoMethods
};
 
PyMODINIT_FUNC
PyInit_ImgProcessing(void) {  // ImgProcessing 为生成的dll名称
    return PyModule_Create(&demoModule);
}

2.Python 代码

把C++ 编译出的ImgProcessing.dll 改为 ImgProcessing.pyd 并 拷贝到Python工程下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# -*- coding:utf-8 -*-
import cv2
import random
import numpy as np
import ImgProcessing as aug
 
 
class AugmentImagesBase:
    def _gauss(self, x, y, sigma=1.):
        Z = 2 * np.pi * sigma ** 2
        kernel_value = 1 / Z * np.exp(-(x ** 2 + y ** 2) / 2 / sigma ** 2)
        return kernel_value
 
    def _gauss_kernel(self, kwidth, kheight, kchannel=1):
        kernels = np.zeros((kheight, kwidth, kchannel, 1), np.float32)
        mid = np.floor(kwidth / 2)
        for kernel_idx in range(kchannel):
            for i in range(kheight):
                for j in range(kwidth):
                    kernels[i, j, kernel_idx, 0] = self._gauss(i - mid, j - mid)
        if kchannel == 1:
            kernels = np.reshape(kernels, (kheight, kwidth))
        return kernels
 
    def left_right_flip(self, img_in, img_out, width=336, height=192):
        aug.AlvaLeftAndRightMirrorImageUInt8(img_in, img_out, width, height)
        return img_out
 
    def up_down_flip(self, img_in, img_out, width=336, height=192):
        aug.AlvaUpAndDownMirrorImageUInt8(img_in, img_out, width, height)
        return img_out
 
    def filtering(self, img_in, img_out, width=336, height=192, kernel=None, kwidth=3, kheight=3):
        aug.AlvaImageFilterFloat(img_in, img_out, width, height, kernel, kwidth, kheight)
        return img_out
 
    def pepper_salt(self, img_in, img_out, width=336, height=192, min_v=0, max_v=255, proportion=0.1):
        rand_proportion = random.uniform(0., proportion)
        aug.AlvaSaltAndPepperUInt8(img_in, img_out, width, height, min_v, max_v, rand_proportion)
        return img_out
 
    def contrast(self, img_in, img_out, width=336, height=192, a=0.6, b=0.4):
        aug.AlvaImageMulAAddBUInt8UInt8(img_in, img_out, width, height, a, b)
        return img_out
 
    def average_rgb(self, img_in, img_out, width=336, height=192):
        img_in = img_in.astype(np.float32)
        img_out = img_out.astype(np.float32)
        aug.AlvaRGBAvgFloatFloat(img_in, img_out, width, height)
        img_out = img_out.astype(np.uint8)
        return img_out
 
    def normalize(self, img_in, img_out, width=336, height=192, type=1):
        aug.AlvaNormalizeUInt8Float(img_in, img_out, width, height, type)
        return img_out
 
    def normal(self, img_in, img_out):
        return img_in
 
    def rota_180(self, img_in, img_out):
        return cv2.rotate(img_in, cv2.ROTATE_180)
 
    def rand_aug(self, img_in):
        img_in = np.asarray(img_in, dtype=np.uint8)
        img_out = np.ones_like(img_in).astype(np.uint8)
        aug_func = {
            "left_right_flip": self.left_right_flip,
            'up_down_flip': self.up_down_flip,
            'pepper_salt': self.pepper_salt,
            'contrast': self.contrast,
            'average_rgb': self.average_rgb,
            "normal": self.normal,
            "rota_180": self.rota_180,
        }
        img_out_curr = np.ones_like(img_in[0]).astype(np.uint8)
        aug_names = []
        for i in range(img_in.shape[0]):
            aug_name = random.sample(list(aug_func.keys()), 1)[0]
            img_out_curr = aug_func[aug_name](np.squeeze(img_in[i]), img_out_curr)
            img_out[i] = img_out_curr
            aug_names.append(aug_name)
        return img_out, aug_names
 
 
def image_aug(img_path):
    import cv2
    import time
    aug_tools = AugmentImagesBase()
    kernel = aug_tools._gauss_kernel(5, 5)
    img_in = cv2.imread(img_path).astype(np.float32)
    img_out = np.ones_like(img_in).astype(np.float32)
    time1 = time.time()
    for i in range(1000):
        # img_out, aug_names = aug_tools.average_rgb(img_in, img_out)
        img_out = aug_tools.filtering(img_in, img_out, kernel=kernel, kwidth=5, kheight=5)
 
    time2 = time.time()
    print("end time:", time2 - time1)
    # cv2.imshow(aug_names[0], img_out[0])
    cv2.imshow("aug img", img_out.astype(np.uint8))
    cv2.imshow('src img', img_in.astype(np.uint8))
    cv2.waitKey(0)
 
 
if __name__ == "__main__":
    img_path = r"G:\20210917\img\1.jpg"
    image_aug(img_path)

PyArg_ParseTuple 的使用见:PyArg_ParseTuple

到此这篇关于Python 调用 C++ 传递numpy 数据详情的文章就介绍到这了,更多相关Python调用 C++内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/jsf921942722/article/details/123799915

延伸 · 阅读

精彩推荐
  • PythonPandas提取单元格的值操作

    Pandas提取单元格的值操作

    这篇文章主要介绍了Pandas提取单元格的值操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    武功震树12332021-10-04
  • Pythonpython PIL模块与随机生成中文验证码

    python PIL模块与随机生成中文验证码

    今天我们要学习的内容是如何利用Python生成一个随机的中文验证码,并将图片保存为.jpeg格式,需要的朋友可以参考下 ...

    Python教程网6292020-08-14
  • Python详解OpenMV图像处理的基本方法

    详解OpenMV图像处理的基本方法

    这篇文章主要介绍了OpenMV图像处理的基本方法,包括感光元件的相关知识介绍,本文给大家介绍的非常详细,需要的朋友可以参考下...

    很注重数学和82111752022-02-24
  • PythonPython绘图库Matplotlib的基本用法

    Python绘图库Matplotlib的基本用法

    这篇文章主要介绍了Python绘图库Matplotlib的基本用法,文中有非常详细的代码示例,对正在学习python的小伙伴们有非常好的帮助,需要的朋友可以参考下...

    Apple-yeran4482021-10-25
  • Pythonpython中如何写类

    python中如何写类

    在本篇文章里小编给大家分享的是一篇关于python中写类的方法和技巧,需要的朋友们可以学习下。...

    silencement6072020-06-30
  • Pythonpython 中的list和array的不同之处及转换问题

    python 中的list和array的不同之处及转换问题

    python中的list是python的内置数据类型,list中的数据类不必相同的,而array的中的类型必须全部相同。这篇文章给大家介绍了python 中的list和array的不同之处及...

    liyaohhh7542021-01-21
  • PythonPython创建普通菜单示例【基于win32ui模块】

    Python创建普通菜单示例【基于win32ui模块】

    这篇文章主要介绍了Python创建普通菜单,结合实例形式分析了Python基于win32ui模块创建普通菜单及添加菜单项的相关操作技巧,并附带说明了win32ui模块的安装命...

    chengqiuming6642021-02-19
  • Pythonpython bmp转换为jpg 并删除原图的方法

    python bmp转换为jpg 并删除原图的方法

    今天小编就为大家分享一篇python bmp转换为jpg 并删除原图的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    sunfellow200912192021-04-12