0

Hi i need to convert an image to b&w with C. I'm on MXLinux. And my idea is to use opencv api. I search on web and i found this way to gain b&w foto. The problem is only when i try to compile it with gcc. It can't found the library opencv, how i can link (permanently not only local) this library?

#include <string.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char** argv){

//read and convert to greyscale
IplImage* im_rgb = cvLoadImage("home.jpg");
IplImage* im_gray = cvCreateImage(cvGetSize(im_rgb),IPL_DEPTH_8U,1);
cvCvtColor(im_rgb,im_gray,CV_RGB2GRAY);

//to black and white
IplImage* im_bw = cvCreateImage(cvGetSize(im_gray),IPL_DEPTH_8U,1);
cvThreshold(im_gray,im_bw,128,255,CV_THRESH_BINARY | CV_THRESH_OTSU);

//save the image
cvSaveImage("image_bw.png",im_bw);
cvSaveImage("image_grey.png",im_gray);
}

The other question is, why doesn't gcc see automatically the library ? After the installation i see the /usr/local/bin and the library are on it!

-----UPDATE1----- 25-02-2021 -----

Yes, after OpenCV 3.x library C seems doesn't exist or maybe doesn't work. Problem solved by install OpenCV 3.4.3.

Compile in this way: gcc NameFile.cpp -o NameFile $ ( pkg-config --cflags --libs opencv )

I obtain an error (i think is caused by cvSaveImage) :

what(): OpenCV(3.4.3) /home/pi/opencv-3.4.3/modules/core/src/matrix_wrap.cpp: 800: error: (-215:Assertion failed) (flags & FIXED_TYPE) != 0 in function 'type'

--SOLVED--

I solved all problem by installing an other version of OpenCV, via web i found that the 3.4.3 version has a bug with matrix_wrap i really don't understand very well the problem but with a clean installation of opencv 3.0.0 all works fine. I get only few error on cmake and make but all are easy to resolve by web.

Thanks to all.

Andrea
  • 1
  • 1
  • 1
    Looks like you are using OpenCV 1.x, with a C API that has been deprecated since 2008. – stateMachine Feb 23 '21 at 23:21
  • Why so old version also deprecated ? – Yunus Temurlenk Feb 24 '21 at 05:00
  • nope i control the version and it's 4.5.1. Yes, i know are deprecate but doesn't mean they don't work – Andrea Feb 24 '21 at 13:25
  • ok i try to follow this https://stackoverflow.com/questions/23821006/issues-compiling-c-not-c-opencv-code but nothing has change The problem is that gcc doesn't recognize the import of cv.h @eldesgraciado – Andrea Feb 25 '21 at 13:02

0 Answers0