about use Mat to make binary image [closed]

asked 2013-10-23 02:17:57 -0600

风飞向何处 gravatar image

updated 2013-10-23 02:37:24 -0600

berak gravatar image

I want make a binary image . the code is the following:

#include<opencv\cv.h>
#include<stdlib.h>
#include<opencv2\opencv.hpp>
#include<opencv\highgui.h>
using namespace cv;
int main()
{
    int i=0,j=0;
    Mat img = imread("hh.jpg");
    if(img.empty())
    {
        printf("无法打开\n");
        return 0;
    }
    for(i=0;i<img.rows;++i)
        for(j=0;j<img.cols;++j)
        {
            if(img.at<uchar>(i,j)>=230)
                img.at<uchar>(i,j)=255;
            else
                img.at<uchar>(i,j)=0;
        }

    imshow("图片浏览",img);
    waitKey();
    return 0;
}
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-10-05 10:07:20.973612

Comments

6
  • load your image as grayscale: Mat img = imread("hh.jpg", 0);
  • then just use: threshold(img,img,230,255);
berak gravatar imageberak ( 2013-10-23 02:41:07 -0600 )edit

thank you for you help!!!

风飞向何处 gravatar image风飞向何处 ( 2013-11-13 21:05:06 -0600 )edit