Ask Your Question
0

Opencv with visual C++2010

asked 2016-10-12 06:20:36 -0600

#include<stdio.h>

include<string.h>

int main(void){

char img_name1[]="";
char img_name2[]="";
printf("画像1をドロップしてください\n");
scanf("%s", img_name1);
printf("画像2をドロップしてください\n");
scanf("%s", img_name2);

// 画像の読み込み
int x2 = 0;
cv::Mat srcImg = cv::imread(img_name1);
printf("gray_img");
cv::Mat gray_img;
printf("グレースケール");
// グレースケールに変換する
cvtColor(srcImg, gray_img, CV_RGB2GRAY);
printf("Mat gray_img_B");
cv::Mat gray_img_B;
// グレースケールに変換する
cvtColor(srcImg, gray_img_B, CV_RGB2GRAY);

cv::Mat srcImg2 = cv::imread(img_name2);
cv::Mat gray_img2;
// グレースケールに変換する
cvtColor(srcImg2, gray_img2, CV_RGB2GRAY);





int count = gray_img_B.rows*gray_img_B.cols;
int color[20000];
int color2[20000];
int color_S[20000];

int hist_G[20000];    //ヒストグラム
int hist_G_S[20000];    //ヒストグラム
int hist_G2[20000];    //ヒストグラム
int hist_G2_S[20000];    //ヒストグラム
int x, y;

//*******************************************
//*               濃淡変換                    *
//*******************************************
x2 = 0;
for (y = 0; y < gray_img.rows; y++) {
    // ポインタの取得
    for (x = 0; x < gray_img.cols; x++) {
        color_S[x2] = gray_img.at<unsigned char>(x, y);

        //符号計算
        if (color_S[x2] >= 0)
            color_S[x2] = 0 - color_S[x2];
        if (color_S[x2]<0)
            color_S[x2] = 0 + color_S[x2];
        gray_img.at<uchar>(x, y) = color_S[x2];
        x2++;

    }
}
cv::namedWindow("入力画像", CV_WINDOW_AUTOSIZE | CV_WINDOW_FREERATIO);
cv::namedWindow("出力画像", CV_WINDOW_AUTOSIZE | CV_WINDOW_FREERATIO);
cv::imshow("入力画像", srcImg);
cv::imshow("出力画像", gray_img);
cv::waitKey(2000);

// 指定したウィンドウを消す
cv::destroyWindow("出力画像");


printf("処理中\n");
//************************************************************************************
x2 = 0;
//*******************************************
//*               値取得                     *
//*******************************************
printf("1\n");
for (y = 0; y < gray_img_B.rows; y++) {
    // ポインタの取得
    for (x = 0; x < gray_img_B.cols; x++) {
        color[x2] = gray_img_B.at<unsigned char>(x, y);
        x2 = x2 + 1;

    }
}

y = 0;

//初期化
for (x = 0; x<count; x++){
    hist_G[x] = 0;
}


bool flag = true;
x2 = 0;

//*******************************************
//*         ヒストグラム作成                   *
//*******************************************
printf("ヒストグラム1\n");
while (flag == true){
    if (color[y] == color[x2]){
        hist_G[y]++;
        x2++;
        y = 0;
        if (x2>count)
            flag = false;
    }
    y++;
}

y = 0;
for (x = 0; x<x2; x++){
    if (hist_G[x]>0){
        hist_G_S[y] = hist_G[x];
        y++;
    }
}



//************************************************************************************


flag = true;
//************************************************************************************
x2 = 0;
//*******************************************
//*               値取得                     *
//*******************************************
printf("2\n");
for (y = 0; y < gray_img_B.rows; y++) {
    // ポインタの取得
    for (x = 0; x < gray_img_B.cols; x++) {
        color2[x2] = gray_img2.at<unsigned char>(x, y);
        x2 = x2 + 1;

    }
}
y = 0;

//初期化
for (x = 0; x<count; x++){
    hist_G2[x] = 0;
}



x2 = 0;

//*******************************************
//*         ヒストグラム作成                   *
//*******************************************
printf("ヒストグラム2\n");
while (flag == true){
    if (color2[y] == color2[x2]){
        hist_G2[y]++;
        x2++;
        y = 0;

        if (x2>count)
            flag = false;
    }
    y++;
}
y = 0;
for (x = 0; x<x2; x++){
    if (hist_G2[x]>0){
        hist_G2_S[y] = hist_G2[x];
        y++;
    }
}
//************************************************************************************


int sum_count[20000];
int sum_count_C = 0;
int num = 0;
float sum_count_C2;
for (x = 0; x<x2; x++){
    sum_count[x] = hist_G_S[x] - hist_G2_S[x];
    sum_count[x] = abs(sum_count[x]);
}

//平均値
for (x = 0; x<x2; x++){
    sum_count_C = sum_count_C + sum_count[x];
}
sum_count_C2 = sum_count_C / x2;
sum_count_C2 = 100 - sum_count_C2;
printf("類似度:%f", sum_count_C2);

return 0;

}

above code cause error.

why?


画像1をドロップしてください C:\Users\user1\Desktop\nCJKAl-388x242.jpg 画像2をドロップしてください C:\Users\user1\Desktop\nCJKAl-388x242.jpg ガゾウヨミコミgray_imgグレースケールOpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor, file C:\builds\2_4_PackSlave-win32-vc12-shared\opencv\modules\imgproc\src\color.cpp, line 3739 続行するには何かキーを押してください . . .

edit retag flag offensive close merge delete

Comments

sidenote: make it a habit, to use (whatever broken) english for comments, var names, strings, etc.

(it's just far easier to get help then)

berak gravatar imageberak ( 2016-10-12 06:34:57 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2016-10-12 06:32:21 -0600

berak gravatar image

updated 2016-10-12 07:02:02 -0600

please check for img.channels() (must be 3) or img.empty() (must be false) after each imread() call.

your image was either not bgr or simply failed to load.

(note, that you can only use ascii strings for filenames here !)

also, you have to reserve memory for your filenames with scanf , like:

char filename[255];

(it might simply be, you're UB lacking that, please see here)

edit flag offensive delete link more

Comments

This program is Success with mac!

why? windows is not success?

nobunagamp5 gravatar imagenobunagamp5 ( 2016-10-12 06:47:59 -0600 )edit

maybe your console win has japanese locale on win, and outputs unicode, not ascii (but i can only guess)

also, please use / or \\ for separator, not \

berak gravatar imageberak ( 2016-10-12 06:51:37 -0600 )edit

error is "OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor, file C:\builds\2_4_PackSlave-win32-vc12-shared\opencv\modules\imgproc\src\color.cpp, line 3739"

I search the channel with img.channels() .

result is 10

nobunagamp5 gravatar imagenobunagamp5 ( 2016-10-12 06:59:24 -0600 )edit

10 is nonsense for channels.

please look at the remarks above the fileenames above, again.

berak gravatar imageberak ( 2016-10-12 07:03:15 -0600 )edit

I change to filename[255]. Then appear the following eroor. it

-------------------------------------------------------------------------------------------------------------------------------------------
OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file c:\users\user1\documents\visual studio 2013\projects\consoleapplication2\packages\opencv.2.4.10\build\native\include\opencv2\core\mat.hpp, line 538
------------------------------------------------------------------------------------------------------------------------

R6010
-abort() has been calle
nobunagamp5 gravatar imagenobunagamp5 ( 2016-10-12 07:12:10 -0600 )edit

mat.at<some_type>(y,x) , not x,y (yea, all typical noob errors present. don't worry, you'll get over it)

berak gravatar imageberak ( 2016-10-12 11:07:50 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-10-12 06:20:36 -0600

Seen: 152 times

Last updated: Oct 12 '16