problem adaptiveThreshold [closed]

asked 2016-06-02 11:55:46 -0500

christianPet gravatar image

hi i have a problem with adaptiveThreshold, i have this error:

OpenCV Error: Assertion failed (src.type() == CV_8UC1) in adaptiveThreshold, file /home/petrucci/opencv-3.1.0/modules/imgproc/src/thresh.cpp, line 1286
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/petrucci/opencv-3.1.0/modules/imgproc/src/thresh.cpp:1286: error: (-215) src.type() == CV_8UC1 in function adaptiveThreshold

the code is:

void cb(uvc_frame_t *frame, void *data) {
  cv::Mat cvFrame(frame->height, frame->width, CV_16UC1, frame->data);
  j++;
  puts("frame");
  ostringstream ss;
        ss << "Image" << j <<"g"<<gain<<".jpg";

        string filename = ss.str();
        imwrite(filename.c_str(),cvFrame);

        Mat image;
        image = imread(filename);
  puts("frame salvato");
  trackFrame((TrackingData*)(data), image);
}



#include "eyetracking.h"

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/photo/photo.hpp>
#include <iostream>
#include <chrono>
#include <utility>

#include "halideFuncs.h"
#include "starburst.h"

static const int kFirstGlintXShadow = 100;
static const int kGlintNeighbourhood = 100;
static const int kEyeRegionWidth = 200;
static const int kEyeRegionHeight = 160;
static const double k8BitScale = (265.0/1024.0)*2.0;

using namespace cv;

struct TrackingData {
  HalideGens *gens;
  TrackingData() {
    gens = createGens();
  }
  ~TrackingData() {
    deleteGens(gens);
  }
};

// search for other set pixels in an area around the point and find the average of the set locations
static Point findLocalCenter(Mat &m, Point p, int size) {
  int xSum = 0;
  int ySum = 0;
  int count = 0;
  for(int i = std::max(0,p.y-size); i < std::min(m.rows,p.y+size); i++) {
    const uint8_t* Mi = m.ptr<uint8_t>(i);
    for(int j = std::max(0,p.x-size); j < std::min(m.cols,p.x+size); j++) {
      if(Mi[j] == 0) {
        xSum += j; ySum += i;
        count += 1;
      }
    }
  }
  if(count == 0) return Point(0,0);
  return Point(xSum/count, ySum/count);
}

static std::vector<Point> trackGlints(TrackingData *dat, Mat &m) {
  // double maxVal;
  // Point maxPt;
  // minMaxLoc(m, nullptr, &maxVal, nullptr, &maxPt);
  // std::cout << "max val: " << maxVal << " at " << maxPt << std::endl;
  // threshold(m, m, maxVal*kGlintThreshold, 255, THRESH_BINARY_INV);
  // adaptiveThreshold(m, m, 1, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 11, -10.0);
  adaptiveThreshold(m, m, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY_INV, 11, -40.0);

  // search for first two pixels separated sufficiently horizontally
  // start from the top and only take the first two so that glints off of teeth and headphones are ignored.
  std::vector<Point> result;
  for(int i = 0; i < m.rows; i++) {
    if(result.size() >= 2) break;
    const uint8_t* Mi = m.ptr<uint8_t>(i);
    for(int j = 0; j < m.cols; j++) {
      if(Mi[j] == 0) {
        if(result.empty()) {
          result.push_back(Point(j,i));
        } else if(j > result[0].x+kFirstGlintXShadow || j < result[0].x-kFirstGlintXShadow) {
          result.push_back(Point(j,i));
          break;
        }
      }
    }
  }
  // Make the found point more centered on the eye instead of being just the first one
  for(auto &&p : result)
    p = findLocalCenter(m,p, kGlintNeighbourhood);

  // consistent order, purely so debug views aren't jittery
  std::sort(result.begin(), result.end(), [](Point a, Point b) {
      return a.x < b.x;
  });

  return result;
}

void trackFrame(TrackingData *dat, Mat &bigM) {
  std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
  start = std::chrono::high_resolution_clock::now();

  // fix stuck pixel on my EyeTribe by ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-12-06 10:11:19.739153

Comments

The error message says, that adaptiveThreshold needs a 8bit single channel image. As far as I can see, you have converted your image to 8bit, so please check if your image has more than one channel.

matman gravatar imagematman ( 2016-06-02 12:49:44 -0500 )edit

I take image from ir cam and they are in greyscale

christianPet gravatar imagechristianPet ( 2016-06-02 14:26:11 -0500 )edit

Thats odd. Nevertheless check if m.type() == CV_8UC1 is true. Sometimes extern grayscale images are embedded in 3 channel or 4 channel (int) container.

matman gravatar imagematman ( 2016-06-02 15:01:19 -0500 )edit

The project is at link https://github.com/trishume/SmartGaze... can you help me?

christianPet gravatar imagechristianPet ( 2016-06-02 15:11:46 -0500 )edit

if i print image.depth() i have 0 value and if i print image.channels() i have 3. how can i change value of channel of image to eliminate the error? the image is in greyscale. if i print image.type() i have 16 value. help me please. If I use "if (image.type ()==8UC3) it's true

christianPet gravatar imagechristianPet ( 2016-06-04 09:35:10 -0500 )edit