cvtColor(src, src_gray, CV_BGR2GRAY)报错
利用cvtColor 进行灰度转换时,不能将 灰度图 转为灰度图,所以在转换前,需对原始图像进行判断。
将这个改成以下
int main(int argc, char* argv[])
{
Mat m = imread("2.jpg", CV_LOAD_IMAGE_COLOR);
IplImage src(m);
cvNamedWindow("showImage");
IplImage* gimg = cvCreateImage(cvGetSize(&src), 8, 1);
cvCvtColor(&src, gimg, CV_BGR2GRAY);
cvSaveImage("1.jpg", gimg);
cvShowImage("showImage", gimg);
cvWaitKey(0);
cvReleaseImage(&gimg);
cvDestroyWindow("showImage");
return 0;
}
完整代码:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h> using namespace cv;
using namespace std; /// Global variables
Mat src, src_gray;
int thresh = 200;
int max_thresh = 255; char* source_window = "Source image";
char* corners_window = "Corners detected"; /// Function header
void cornerHarris_demo(int, void*);
void cvtColor_src(Mat &src, Mat &dst); /** @function main */
int main(int argc, char** argv)
{
/// Load source image and convert it to gray
src = imread("E:\\VS2015Opencv\\vs2015\\project\\picture\\06.jpg"); // 转换单通道
cvtColor_src(src,src_gray); /// Create a window and a trackbar
namedWindow(source_window, CV_WINDOW_AUTOSIZE);
createTrackbar("Threshold: ", source_window, &thresh, max_thresh, cornerHarris_demo);
imshow(source_window, src); cornerHarris_demo(0, 0); waitKey(0);
return(0);
} void cvtColor_src(Mat &src, Mat &src_gray)
{
// 转换单通道
if (src.channels() == 4) {
cv::cvtColor(src, src_gray, CV_BGRA2GRAY);
}
else if (src.channels() == 3) {
cv::cvtColor(src, src_gray, CV_BGR2GRAY);
}
else if (src.channels() == 2) {
cv::cvtColor(src, src_gray, CV_BGR5652GRAY);
}
else if (src.channels() == 1) {// 单通道的图片直接就不需要处理
src_gray = src;
}
else { // 负数,说明图有问题 直接返回
src_gray = src;
} } /** @function cornerHarris_demo */
void cornerHarris_demo(int, void*)
{ Mat dst, dst_norm, dst_norm_scaled;
dst = Mat::zeros(src.size(), CV_32FC1); /// Detector parameters
int blockSize = 2;
int apertureSize = 3;
double k = 0.04; /// Detecting corners
cornerHarris(src_gray, dst, blockSize, apertureSize, k, BORDER_DEFAULT); /// Normalizing
normalize(dst, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat());
convertScaleAbs(dst_norm, dst_norm_scaled); /// Drawing a circle around corners
for (int j = 0; j < dst_norm.rows; j++)
{
for (int i = 0; i < dst_norm.cols; i++)
{
if ((int)dst_norm.at<float>(j, i) > thresh)
{
circle(dst_norm_scaled, Point(i, j), 5, Scalar(0), 2, 8, 0);
}
}
}
/// Showing the result
namedWindow(corners_window, CV_WINDOW_AUTOSIZE);
imshow(corners_window, dst_norm_scaled);
}
cvtColor(src, src_gray, CV_BGR2GRAY)报错的更多相关文章
- Android Studio中使用android:src="@drawable/ic_launcher"报错
今天尝试着安装了Android Studio,界面确实不错,列表什么的也改了很多. 然后新建工程,习惯性在activity_main那里加上一段代码测试看看: <ImageView androi ...
- 使用 Git 报错 error: src refspec master matches more than one.
今天在使用 Git push 代码时遇到一个报错: error: src refspec master matches more than one. error: failed to push som ...
- laravel5.5 使用alipay SDK报错Cannot redeclare Encrypt() (previously declared in ../vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:448)
错误现象: 在laravel5.5 中,使用alipaySDK 报错: Cannot redeclare Encrypt() (previously declared in ../vendor/lar ...
- Maven Web Projest经Update Projest报错:Cannot nest 'myApp/src/main/resource' inside 'myApp/src'. To enable the nesting exclude 'main/' from 'myApp/src'
1,报错场景 2,解决方法 修改pom.xml,去掉该行:<sourceDirectory>src/</sourceDirectory>
- Git提交代码报错Git push error:src refspec XXX matches more than one解决方案
Git提交代码push时,报错这个 error: src refspec master matches more than one. error: failed to push some refs t ...
- git报错:src refspec master does not match any
问题出现:git推送本地内容到远程仓库时,报错src refspec master does not match any. 1.我的流程: mkdir project_k命令,新建本地仓库. cd p ...
- onerror="javascript:this.src='images/defaultUpload.png';"【容易导致死循环报错】
当无法找到默认图片时,onerror="javascript:this.src='images/defaultUpload.png';"容易导致死循环报错
- 报错:OpenCV Error: Assertion failed (src.size() == dst.size() && src.type() == dst.ty pe()) in unknown function, file ..……
在用cvDilate函数的时候,老是导致程序中断,报错如下: OpenCV Error: Assertion failed (src.size() == dst.size() && s ...
- Pytorch报错:cuda runtime error (59) : device-side assert triggered at /pytorch/aten/src/THC/generic/THCTensorMath.cu:26
Pytorch报错:cuda runtime error (59) : device-side assert triggered at /pytorch/aten/src/THC/generic/TH ...
随机推荐
- Java(一)环境的安装与配置
一.JDK的安装 1.JDK与JRE的区别 Java开发环境,简称JDK(Java Development Kit),它是Java的核心,包括了Java编译器.Java运行环境.Java打包工具.Ja ...
- [CF1303A] Erasing Zeroes
Solution 找到边界然后循环扫一遍数个数即可 #include <bits/stdc++.h> using namespace std; int n; const int N = 1 ...
- ArcGIS Server Manager 重置密码
重置忘记的密码 ArcGIS 10.5 (Windows) | 其他版本 根据以下说明为标识存储中的用户或主站点管理员帐户重置密码. 为标识存储中的用户重置密码 可按照以下步骤为标识存储中的用户重置密 ...
- 48.Python中ORM模型实现mysql数据库基本的增删改查操作
首先需要配置settings.py文件中的DATABASES与数据库的连接信息, DATABASES = { 'default': { 'ENGINE': 'django.db.backends.my ...
- Dubbo之服务消费
Dubbo的服务消费主要包括两个部分.第一大步是ReferenceConfig类的init方法调用Protocol的refer方法生成Invoker实例,这是服务消息的关键.第二大步是把Invoker ...
- Cannot resolve collation conflict between "Chinese_Taiwan_Stroke_CI_AS" and "Chinese_PRC_CI_AS" in UNION ALL operator occurring in SELECT statement column 1.
Cannot resolve collation conflict between . 解决方案: COLLATE Chinese_PRC_CI_AS 例子: SELECT A.Name FROM A ...
- Ubuntu16.04突然断网
配置文件: sudo gedit /etc/network/interfaces 重启网络: 1.sudo /etc/init.d/networking restart 2.sudo service ...
- pandas 读取excel时,遇到数字变为科学计数法了, 怎么破?? 别慌 这样来处理
# 指定字段以string 方式读取 df = pd.read_excel("./test.xlsx", converters={"id": str})
- Failed to set locale, defaulting to C
echo "export LC_ALL=en_US.UTF-8" >> /etc/profile source /etc/profile 没有设置local环境
- 番外:你真的了解 Oracle 的启动流程吗?
番外系列说明:该系列所有文章都将作为独立篇章进行知识点讲解,是对其他系列博文进行的补充说明,来自于博客园AskScuti. 主题:关于数据库启动流程的三个阶段 内容预览:本篇涉及数据库启动的三个阶段分 ...