[英]“Fatal signal 11 (SIGSEGV), code 1” when using mixChannels function (opencv)
我正在嘗試使用Android JNI和OpenCV開發應用程序,但遇到了一個嚴重錯誤:libc:致命信號11(SIGSEGV),代碼1,在tid 28694中出現了錯誤addr 0xfffffffc。
我按照本文來實現該算法,當我在Visual Studio 2015上運行它時,它運行良好。 但是當我嘗試在Android JNI上實現它時,它出現了以下錯誤。
這是我的代碼:
#include <jni.h>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
extern "C" {
JNIEXPORT void JNICALL
Java_com_noah_demojniexp_MainActivity_autofix(JNIEnv *env, jobject instance, jlong matAddr,
jlong dstAddr, jfloat clipHistPercent) {
Mat &src = *((Mat *) matAddr);
Mat &dst = *((Mat *) dstAddr);
CV_Assert(clipHistPercent >= 0);
CV_Assert((src.type() == CV_8UC1) || (src.type() == CV_8UC3) || (src.type() == CV_8UC4));
int histSize = 256;
float alpha, beta;
double minGray = 0, maxGray = 0;
//to calculate grayscale histogram
Mat gray;
if (src.type() == CV_8UC1) gray = src;
else if (src.type() == CV_8UC3) cvtColor(src, gray, CV_BGR2GRAY);
else if (src.type() == CV_8UC4) cvtColor(src, gray, CV_BGRA2GRAY);
if (clipHistPercent == 0)
{
// keep full available range
minMaxLoc(gray, &minGray, &maxGray);
}
else
{
Mat hist; //the grayscale histogram
float range[] = { 0, 256 };
const float* histRange = { range };
bool uniform = true;
bool accumulate = false;
calcHist(&gray, 1, 0, Mat (), hist, 1, &histSize, &histRange, uniform, accumulate);
// calculate cumulative distribution from the histogram
std::vector<float> accumulator(histSize);
accumulator[0] = hist.at<float>(0);
for (int i = 1; i < histSize; i++)
{
accumulator[i] = accumulator[i - 1] + hist.at<float>(i);
}
// locate points that cuts at required value
float max = accumulator.back();
clipHistPercent *= (max / 100.0); //make percent as absolute
clipHistPercent /= 2.0; // left and right wings
// locate left cut
minGray = 0;
while (accumulator[minGray] < clipHistPercent)
minGray++;
// locate right cut
maxGray = histSize - 1;
while (accumulator[maxGray] >= (max - clipHistPercent))
maxGray--;
}
// current range
float inputRange = maxGray - minGray;
alpha = (histSize - 1) / inputRange; // alpha expands current range to histsize range
beta = -minGray * alpha; // beta shifts current range so that minGray will go to 0
// Apply brightness and contrast normalization
// convertTo operates with saurate_cast
src.convertTo(dst, -1, alpha, beta);
// restore alpha channel from source
if (dst.type() == CV_8UC4)
{
int from_to[] = { 3, 3};
mixChannels(&src, 4, &dst,1, from_to, 1);
}
return;
}
}
我創建了一個擴展org.opencv.core.Mat的類
public class SMat extends Mat {
public void autofix(Mat m,float clip){
MainActivity.native_autofix(nativeObj,m.nativeObj,clip);
}
}
並在mainactivity中使用它
Bitmap bm1 = BitmapFactory.decodeResource(getResources(),R.mipmap.s3);
SMat mat1 = new SMat();
SMat mat2 = new SMat();
Utils.bitmapToMat(bm1,mat1);
mat1.autofix(mat2,5);
Bitmap bm2 = Bitmap.createBitmap(bm1);
Utils.matToBitmap(mat1,bm2);
iv2.setImageBitmap(bm2);
調試時,我發現mixChannels函數是原因,如果對其進行注釋,一切都會好起來的。 我不知道為什么會出錯。 請幫助! 感謝大家!
源矩陣和目標矩陣必須具有相同的大小和深度。
您可以從源矩陣中克隆目標矩陣的大小,例如
Mat dst = src.clone();
在目標字段中,它具有與原始圖像相同的大小和深度,然后再將其傳遞給mixChannels函數
我找到了解決方案,只需將第二個參數更改為1,因為源Mat僅包含一個Mat。 :D
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.