退会済みユーザー
2017/07/09 08:53 編集
間違い
Q&A
退会済みユーザー
総合スコア0
投稿2017/07/09 08:23
編集2017/07/09 08:31C++
1#include <stdio.h> 2#include <stdlib.h> 3#include <highgui.h> 4#include <cv.h> 5#include <cxcore.h> 6 7#define _DEBUG_IMG 0 8 9// テキトウな円形検出フィルター 10#define CIRCLE_FILTER_WIDTH 16 11#define CIRCLE_FILTER_HEIGHT 16 12const int CIRCLE_FILTER[CIRCLE_FILTER_HEIGHT][CIRCLE_FILTER_WIDTH] = 13{ 14 { -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3 }, 15 { -3, -3, -3, -3, +1, +1, +1, +1, +1, +1, +1, +1, -3, -3, -3, -3 }, 16 { -3, -3, -3, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, -3, -3, -3 }, 17 { -3, -3, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, -3, -3 }, 18 { -3, -3, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, -3, -3 }, 19 { -3, -3, +1, +1, +1, +1, +2, +2, +2, +2, +1, +1, +1, +1, -3, -3 }, 20 { -3, -3, +1, +1, +1, +2, +2, +2, +2, +2, +2, +1, +1, +1, -3, -3 }, 21 { -3, -3, +1, +1, +1, +2, +2, +2, +2, +2, +2, +1, +1, +1, -3, -3 }, 22 { -3, -3, +1, +1, +1, +2, +2, +2, +2, +2, +2, +1, +1, +1, -3, -3 }, 23 { -3, -3, +1, +1, +1, +2, +2, +2, +2, +2, +2, +1, +1, +1, -3, -3 }, 24 { -3, -3, +1, +1, +1, +1, +2, +2, +2, +2, +1, +1, +1, +1, -3, -3 }, 25 { -3, -3, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, -3, -3 }, 26 { -3, -3, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, -3, -3 }, 27 { -3, -3, -3, +1, +1, +1, +1, +1, +1, +1, +1, +1, +1, -3, -3, -3 }, 28 { -3, -3, -3, -3, +1, +1, +1, +1, +1, +1, +1, +1, -3, -3, -3, -3 }, 29 { -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3 } 30}; 31// 円形かの閾値 32#define CIRCLE_FILTER_THRESHOLD 110 33 34// 青色の範囲 35#define HSV_BLUE_LOWER cvScalar(40, 70, 70) 36#define HSV_BLUE_UPPER cvScalar(80, 255, 255) 37 38// 円形? 39int isCircle(int val) 40{ 41 return CIRCLE_FILTER_THRESHOLD < val ? 1:0; 42} 43 44// 青色ボール検出 45int blueBallDetect() 46{ 47 CvCapture *capture = 0; 48 capture = cvCaptureFromCAM (0); 49 IplImage *base_cap = cvQueryFrame (capture); 50 IplImage *hsv_cap = cvCreateImage(cvGetSize(base_cap), IPL_DEPTH_8U, 3); 51 IplImage *blue_base_cap = cvCreateImage(cvGetSize(base_cap), IPL_DEPTH_8U, 1); 52 IplImage *blue_cap = cvCreateImage(cvGetSize(base_cap), IPL_DEPTH_8U, 1); 53 IplImage *temp_cap; 54 55 int count = 0; 56 double scale = 1.3; 57 double current_scale = 1; 58 59 // 青色領域抽出 60 CvScalar blue_lower = HSV_BLUE_LOWER; 61 CvScalar blue_upper = HSV_BLUE_UPPER; 62 cvCvtColor(base_cap, hsv_cap , CV_BGR2HSV); 63 cvInRangeS(hsv_cap, blue_lower, blue_upper, blue_base_cap ); 64 cvCopy(blue_base_cap, blue_cap ); 65 66 // 探索 67 while (CIRCLE_FILTER_WIDTH < blue_cap ->width 68 && CIRCLE_FILTER_HEIGHT < blue_cap ->height) 69 { 70 for (int y = 0; y + CIRCLE_FILTER_HEIGHT < blue_cap ->height; y += 2) { 71 for (int x = 0; x + CIRCLE_FILTER_WIDTH < blue_cap ->width; x += 2) { 72 // フィルター出力値計算 73 int filter_value = 0; 74 75 for (int fy = 0; fy < CIRCLE_FILTER_HEIGHT; ++fy ) { 76 for (int fx = 0; fx < CIRCLE_FILTER_WIDTH; ++fx ) { 77 CvScalar color = cvGet2D( 78 blue_cap , 79 y + fy, 80 x + fx 81 ); 82 filter_value += CIRCLE_FILTER[fy][fx] * (color.val[0] == 0 ? 0:1); 83 } 84 } 85 86 if (isCircle(filter_value)) { 87 // ヒット 88 int rx = cvRound(x * current_scale); 89 int ry = cvRound(y * current_scale); 90 int rw = cvRound(CIRCLE_FILTER_WIDTH * current_scale); 91 int rh = cvRound(CIRCLE_FILTER_HEIGHT * current_scale); 92#ifdef _DEBUG 93 printf("filter value: %d\n", filter_value); 94#endif 95#if _DEBUG_IMG 96 cvCircle( 97 blue_cap , 98 cvPoint(x + (CIRCLE_FILTER_WIDTH / 2), y + (CIRCLE_FILTER_HEIGHT / 2)), 99 CIRCLE_FILTER_WIDTH, 100 cvScalar(0xcc), 2 101 ); 102#endif 103 // マーク 104 cvCircle( 105 base_cap , 106 cvPoint(rx + (rw / 2), ry + (rh /2)), 107 (rw / 2), 108 CV_RGB(255, 255, 255), 2 109 ); 110 111 ++count; 112 } 113 } 114 } 115#if _DEBUG_IMG 116 cvShowImage("BASE", blue_cap ); 117 cvWaitKey(); 118#endif 119 temp_cap = blue_cap ; 120 blue_cap = cvCreateImage( 121 cvSize( 122 cvRound(blue_cap->width / scale), 123 cvRound(blue_cap->height / scale) 124 ), 125 IPL_DEPTH_8U, 1 126 ); 127 cvResize(blue_base_cap, blue_cap); 128 cvReleaseImage(&temp_cap); 129 current_scale *= scale; 130 } 131 cvReleaseImage(&blue_cap); 132 cvReleaseImage(&blue_base_cap); 133 cvNamedWindow("BASE"); 134 cvShowImage("BASE", base_cap); 135 cvWaitKey(); 136 cvReleaseImage(&blue_cap); 137 cvReleaseImage(&blue_base_cap); 138 return count; 139} 140 141int main() 142{ 143 blueBallDetect(); 144 return EXIT_SUCCESS; 145}
パソコンのカメラで読み込んだ画像から青い円を検出するプログラムを書いたのですがOpenCV Error: Bad argument (Array should be CvMat or IplImage) in cvGetSize, file /tmp/opencv3-20161231-756-1ihi7x0/opencv-3.2.0/modules/core/src/array.cpp, line 1255
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /tmp/opencv3-20161231-756-1ihi7x0/opencv-3.2.0/modules/core/src/array.cpp:1255: error: (-5) Array should be CvMat or IplImage in function cvGetSizeというエラーが出てしまいます
どうすればいいでしょうか。パソコンはmacです
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。