GVKun编程网logo

错误: (-215:Assertion failed) !_src.empty() 在函数 'cv::cvtColor' 中但没有使用 cvt.COLOR()

11

本文将为您提供关于错误:(-215:Assertionfailed)!_src.empty()在函数'cv::cvtColor'中但没有使用cvt.COLOR()的详细介绍,同时,我们还将为您提供关于

本文将为您提供关于错误: (-215:Assertion failed) !_src.empty() 在函数 'cv::cvtColor' 中但没有使用 cvt.COLOR()的详细介绍,同时,我们还将为您提供关于!_src.empty() 函数 'cv::cvtColor' imgprocsrccolor.cpp:182、Android 中使用 OpenCV3.3.0 进行图片比较报错。OpenCV Error:Assertion failed、c – 使用ARM NEON内在函数的cvtColor的SIMD优化、c – 使用cvtColor转换单一颜色的实用信息。

本文目录一览:

错误: (-215:Assertion failed) !_src.empty() 在函数 'cv::cvtColor' 中但没有使用 cvt.COLOR()

错误: (-215:Assertion failed) !_src.empty() 在函数 'cv::cvtColor' 中但没有使用 cvt.COLOR()

如何解决错误: (-215:Assertion failed) !_src.empty() 在函数 ''cv::cvtColor'' 中但没有使用 cvt.COLOR()?

即使我没有使用 cvt.COLOR(),我也收到了这个错误,只是一个读取和显示视频的基本代码。

import  cv2
import mediapipe as mp

cap = cv2.VideoCapture("C:/Users/swastik/Desktop/python_project/cv_projects/Advance/PoseVideos1.mp4")
while True:
    success,img = cap.read()
    cv2.imshow(''video'',img)
    cv2.waitKey(1)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

!_src.empty() 函数 'cv::cvtColor' imgprocsrccolor.cpp:182

!_src.empty() 函数 'cv::cvtColor' imgprocsrccolor.cpp:182

如何解决!_src.empty() 函数 ''cv::cvtColor'' imgproc\src\color.cpp:182?

PyCharm 中的相机错误。在这种情况下我该怎么办?

import cv2
import numpy as np
frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture(1)
cap.set(3,frameWidth)
cap.set(4,frameHeight)
cap.set(10,150)
while True:
    success,img = cap.read()
    cv2.imshow("Result",img)
    if cv2.waitKey(1) and 0xFF == ord(''q''):
     break
C:\Users\user\PycharmProjects\OpenCvpython\venv\Scripts\python.exe C:/Users/user/PycharmProjects/OpenCvpython/chapter7.py
[ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ep71p_ws\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace''::SourceReaderCB::~SourceReaderCB terminating async callback
Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/OpenCvpython/chapter7.py",line 11,in <module>
    cv2.imshow("Result",img)
cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ep71p_ws\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion Failed) !_src.empty() in function ''cv::cvtColor''


Process finished with exit code 1

解决方法

先检查是否成功检索到视频捕获帧,例如:

import cv2
import numpy as np
frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture(1)
cap.set(3,frameWidth)
cap.set(4,frameHeight)
cap.set(10,150)
while True:
    success,img = cap.read()

    if not success:
      print(''error retrieving frame'')
      continue

    cv2.imshow("Result",img)
    if cv2.waitKey(1) and 0xFF == ord(''q''):
     break

Android 中使用 OpenCV3.3.0 进行图片比较报错。OpenCV Error:Assertion failed

Android 中使用 OpenCV3.3.0 进行图片比较报错。OpenCV Error:Assertion failed

opencv-3.3.0-android-sdk
AndroidStudio2.3.3
Nexus6, Android6.0

private void compareImg(Bitmap bitmap1, Bitmap bitmap2){ 
    Mat mat1 = new Mat(); 
    Mat mat2 = new Mat(); 
 
    Utils.bitmapToMat(bitmap1, mat1); // 将bitmap数据放入Mat 
    Utils.bitmapToMat(bitmap2, mat2); //  
 
    Mat matGray1 = new Mat(); 
    Mat matGray2 = new Mat(); 
 
    Imgproc.cvtColor(mat1, matGray1, Imgproc.COLOR_BGR2GRAY); // 转换彩色数据为灰度数据 
    Imgproc.cvtColor(mat2, matGray2, Imgproc.COLOR_BGR2GRAY); 
 
    matGray1.convertTo(matGray1, CvType.CV_32F); 
    matGray2.convertTo(matGray2, CvType.CV_32F); 
 
    double target = Imgproc.compareHist(matGray1, matGray2, Imgproc.CV_COMP_CORREL); // 报错的代码 
}

出现的错误反馈:

com.cuiweiyou.testopencv E/cv::error(): 
  OpenCV Error: 
    Assertion failed (A.size == arrays[i0]->size) 
    in void cv::NAryMatIterator::init(const cv::Mat**, cv::Mat*, uchar**, int), 
    file /build/master  pack-android/opencv/modules/core/src/matrix.cpp, line 4790

com.cuiweiyou.testopencv E/org.opencv.imgproc: 
  imgproc::compareHist  10() caught cv::Exception: /build/master  pack-android/opencv/modules/core/src/matrix.cpp:4790: 
    error: (-215) A.size == arrays[i0]->size 
    in function void cv::NAryMatIterator::init(const cv::Mat**, cv::Mat*, uchar**, int)

com.cuiweiyou.testopencv E/AndroidRuntime: 
  FATAL EXCEPTION: 
    main Process: com.cuiweiyou.testopencv, PID: 9817
    CvException [org.opencv.core.CvException: cv::Exception: /build/master  pack-android/opencv/modules/core/src/matrix.cpp:4790: 
      error: (-215) A.size == arrays[i0]->size 
        in function void cv::NAryMatIterator::init(const cv::Mat**, cv::Mat*, uchar**, int)]
      at org.opencv.imgproc.Imgproc.compareHist  0(Native Method)
      at org.opencv.imgproc.Imgproc.compareHist(Imgproc.java:767)

我尝试修改这行代码里的最后参数 "Imgproc.cvtColor (mat1, matGray1, Imgproc.COLOR BGR2GRAY);",使用 "Imgproc.COLOR BGRA2GRAY" 或其它几个也失败。

请网友帮忙,谢谢!
 

c – 使用ARM NEON内在函数的cvtColor的SIMD优化

c – 使用ARM NEON内在函数的cvtColor的SIMD优化

我正在使用BGR的SIMD优化来进行灰度转换,相当于 OpenCV’s cvtColor() function.有一个Intel SSE版本的这个功能,我指的是它. (我正在做的是基本上将SSE代码转换为NEON代码.)

我几乎完成了编写代码,可以用g编译它,但是我无法得到正确的输出.有没有人有任何想法可能是什么错误?

我得到什么(不正确):

我应该得到什么

这是我的代码:

#include <opencv/cv.hpp>
#include <opencv/highgui.h>
#include <arm_neon.h>
//#include <iostream>

using namespace std;
//using namespace cv;

#define int8x16_to_8x8x2(v) ((int8x8x2_t) { vget_low_s8(v),vget_high_s8(v) })

void cvtBGR2GrayNEON(cv::Mat& src,cv::Mat& dest)
{
  const int size = src.size().area()*src.channels();
  uchar* s = src.ptr<uchar>(0);
  uchar* d = dest.ptr<uchar>(0);

  const int8x16_t mask1 = {0,3,6,9,12,15,1,4,7,10,13,2,5,8,11,14};
  const int8x16_t smask1 = {6,14,15};
  const int8x16_t ssmask1 = {11,10};

  const int8x16_t mask2 = {0,13};
  const int8x16_t ssmask2 = {0,10};

  const int8x16_t bmask1 = {255,255,0};
  const int8x16_t bmask2 = {255,0};
  const int8x16_t bmask3 = {255,0};
  const int8x16_t bmask4 = {255,0};

  const int shift = 8;
  const int amp = 1<<shift;

  const int16_t _R_ = (int16_t)(amP*0.299);
  const int16_t _G_ = (int16_t)(amP*0.587);
  const int16_t _B_ = (int16_t)(amP*0.114);
  const int16x8_t R = vdupq_n_s16(_R_);
  const int16x8_t G = vdupq_n_s16(_G_);
  const int16x8_t B = vdupq_n_s16(_B_);
  const int8x16_t zero = vdupq_n_s8(0);

  for(int i = 0; i < size; i += 48)
    {
      int8x16_t a = vld1q_s8((int8_t *) s + i);
      int8x16_t b = vld1q_s8((int8_t *) s + i + 16);
      int8x16_t c = vld1q_s8((int8_t *) s + i + 32);

      a = vcombine_s8(vtbl2_s8(int8x16_to_8x8x2(a),vget_low_s8(mask1)),vtbl2_s8(int8x16_to_8x8x2(a),vget_high_s8(mask1)));
      b = vcombine_s8(vtbl2_s8(int8x16_to_8x8x2(b),vget_low_s8(mask2)),vtbl2_s8(int8x16_to_8x8x2(b),vget_high_s8(mask2)));
      c = vcombine_s8(vtbl2_s8(int8x16_to_8x8x2(c),vtbl2_s8(int8x16_to_8x8x2(c),vget_high_s8(mask2)));

      //BBBBBB
      const int8x16_t aaaa = vbslq_s8(c,vbslq_s8(b,a,bmask1),bmask2);

      a = vcombine_s8(vtbl2_s8(int8x16_to_8x8x2(a),vget_low_s8(smask1)),vget_high_s8(smask1)));
      b = vcombine_s8(vtbl2_s8(int8x16_to_8x8x2(b),vget_high_s8(smask1)));
      c = vcombine_s8(vtbl2_s8(int8x16_to_8x8x2(c),vget_high_s8(smask1)));

      //GGGGGG
      const int8x16_t bbbb = vbslq_s8(c,bmask3),vget_low_s8(ssmask1)),vget_high_s8(ssmask1)));
      c = vcombine_s8(vtbl2_s8(int8x16_to_8x8x2(c),vget_high_s8(ssmask1)));
      b = vcombine_s8(vtbl2_s8(int8x16_to_8x8x2(b),vget_low_s8(ssmask2)),vget_high_s8(ssmask2)));

      //RRRRRR
      const int8x16_t cccc = vbslq_s8(c,bmask4);

      /*
      int8x8x2_t a1 = vzip_s8(vget_high_s8(aaaa),vget_high_s8(zero));
      int8x8x2_t a2 = vzip_s8(vget_low_s8(aaaa),vget_low_s8(zero));
      */

      int8x16_t a1 = aaaa;
      int8x16_t a2 = zero;
      int8x16x2_t temp1 =  vzipq_s8(a1,a2);
      a1 = temp1.val[0];
      a2 = temp1.val[1];
      int16x8_t aa1 = vmulq_s16((int16x8_t)a2,B);
      int16x8_t aa2 = vmulq_s16((int16x8_t)a1,B);

      int8x16_t b1 = bbbb;
      int8x16_t b2 = zero;
      int8x16x2_t temp2 =  vzipq_s8(b1,b2);
      b1 = temp2.val[0];
      b2 = temp2.val[1];
      int16x8_t bb1 = vmulq_s16((int16x8_t)b2,G);
      int16x8_t bb2 = vmulq_s16((int16x8_t)b1,G);

      int8x16_t c1 = cccc;
      int8x16_t c2 = zero;
      int8x16x2_t temp3 =  vzipq_s8(c1,c2);
      c1 = temp3.val[0];
      c2 = temp3.val[1];
      int16x8_t cc1 = vmulq_s16((int16x8_t)c2,R);
      int16x8_t cc2 = vmulq_s16((int16x8_t)c1,R);

      aa1 = vaddq_s16(aa1,bb1);
      aa1 = vaddq_s16(aa1,cc1);
      aa2 = vaddq_s16(aa2,bb2);
      aa2 = vaddq_s16(aa2,cc2);

      const int shift1 = 8;
      aa1 = vshrq_n_s16(aa1,shift1);
      aa2 = vshrq_n_s16(aa2,shift1);

      uint8x8_t aaa1 = vqmovun_s16(aa1);
      uint8x8_t aaa2 = vqmovun_s16(aa2);

      uint8x16_t result = vcombine_u8(aaa1,aaa2);

      vst1q_u8((uint8_t *)(d),result);

      d+=16;
    }    
}

int main() 
{
  cv::Mat src = cv::imread("Lenna.bmp");
  cv::Mat dest(src.rows,src.cols,CV_8UC1);

  cvtBGR2GrayNEON(src,dest);

  cv::imwrite("grey.jpg",dest);

  return 0;
}

这是等效的SSE代码(从here):

void cvtBGR2GraySSEShort(Mat& src,Mat& dest)
{
    const int size = src.size().area()*src.channels();
    uchar* s = src.ptr<uchar>(0);
    uchar* d = dest.ptr<uchar>(0);

    //data structure
    //BGR BGR BGR BGR BGR B
    //GR BGR BGR BGR BGR BG
    //R BGR BGR BGR BGR BGR
    //shuffle to BBBBBBGGGGGRRRRR
    const __m128i mask1 = _mm_setr_epi8(0,14);
    const __m128i smask1 = _mm_setr_epi8(6,15);
    const __m128i ssmask1 = _mm_setr_epi8(11,10);

    //shuffle to GGGGGGBBBBBRRRRR
    const __m128i mask2 = _mm_setr_epi8(0,13);
    //const __m128i smask2 = _mm_setr_epi8(6,15);same as smask1
    const __m128i ssmask2 = _mm_setr_epi8(0,10);

    //shuffle to RRRRRRGGGGGBBBBB
    //__m128i mask3 = _mm_setr_epi8(0,13);//same as mask2
    //const __m128i smask3 = _mm_setr_epi8(6,10);//same as smask1
    //const __m128i ssmask3 = _mm_setr_epi8(11,10);//same as ssmask1

    //blend mask
    const __m128i bmask1 = _mm_setr_epi8
        (255,0);

    const __m128i bmask2 = _mm_setr_epi8
        (255,0);

    const __m128i bmask3 = _mm_setr_epi8
        (255,0);

    const __m128i bmask4 = _mm_setr_epi8
        (255,0);  

    const int shift = 8;
    const int amp = 1<<shift;
    const int _R_=(int)(amP*0.299);
    const int _G_=(int)(amP*0.587);
    const int _B_=(int)(amP*0.114);
    const __m128i R = _mm_set1_epi16(_R_);
    const __m128i G = _mm_set1_epi16(_G_);
    const __m128i B = _mm_set1_epi16(_B_);
    const __m128i zero = _mm_setzero_si128();   

    for(int i=0;i<size;i+=48)
    {
        __m128i a = _mm_shuffle_epi8(_mm_load_si128((__m128i*)(s+i)),mask1);
        __m128i b = _mm_shuffle_epi8(_mm_load_si128((__m128i*)(s+i+16)),mask2);
        __m128i c = _mm_shuffle_epi8(_mm_load_si128((__m128i*)(s+i+32)),mask2);
        const __m128i aaaa = _mm_blendv_epi8(c,_mm_blendv_epi8(b,bmask2);

        a = _mm_shuffle_epi8(a,smask1);
        b = _mm_shuffle_epi8(b,smask1);
        c = _mm_shuffle_epi8(c,smask1);
        const __m128i bbbb =_mm_blendv_epi8(c,ssmask1);
        c = _mm_shuffle_epi8(c,ssmask1);
        b = _mm_shuffle_epi8(b,ssmask2);
        const __m128i cccc =_mm_blendv_epi8(c,bmask4);

        __m128i a1 = _mm_unpackhi_epi8(aaaa,zero);
        __m128i a2 = _mm_unpacklo_epi8(aaaa,zero);
        a1 = _mm_mullo_epi16(a1,B);
        a2 = _mm_mullo_epi16(a2,B);
        __m128i b1 = _mm_unpackhi_epi8(bbbb,zero);
        __m128i b2 = _mm_unpacklo_epi8(bbbb,zero);
        b1 = _mm_mullo_epi16(b1,G);
        b2 = _mm_mullo_epi16(b2,G);

        __m128i c1 = _mm_unpackhi_epi8(cccc,zero);
        __m128i c2 = _mm_unpacklo_epi8(cccc,zero);
        c1 = _mm_mullo_epi16(c1,R);
        c2 = _mm_mullo_epi16(c2,R);

        a1 = _mm_add_epi16(a1,b1);
        a1 = _mm_add_epi16(a1,c1);
        a2 = _mm_add_epi16(a2,b2);
        a2 = _mm_add_epi16(a2,c2);

        a1 = _mm_srli_epi16(a1,8);
        a2 = _mm_srli_epi16(a2,8);

        a = _mm_packus_epi16(a1,a2);

        _mm_stream_si128((__m128i*)(d),a);
        d+=16;
    } 
}

解决方法

好的,下面是我刚刚写的那个函数的完全优化版本(请注意,如果大小小于32,则此函数只返回)
/*
 *  Created on: 2014. 7. 27.
 *      Author: Jake Lee
 *      Project FANIC - Fastest ARM NEON Implementaion Challenge
 */

// void fanicCvtBGR2GrayNEON(void *pDst,void *pSrc,unsigned int size);
// Y = 0.114*B + 0.587*G + 0.299*R
    .text
    .arm
    .global fanicCvtBGR2GrayNEON

    pDst    .req    r0
    pSrc    .req    r1
    size    .req    r2

    .align 5
    .func
fanicCvtBGR2GrayNEON:
    pld     [pSrc]
    subs    size,size,#32
    pld     [pSrc,#64]
    bxmi    lr
    pld     [pSrc,#64*2]
    vmov.i8     d0,#29
    vmov.i8     d1,#150
    vmov.i8     d2,#77

    .align 5
1:
    vld3.8      {d20,d21,d22},[pSrc]!
    vld3.8      {d23,d24,d25},[pSrc]!
    vld3.8      {d26,d27,d28},[pSrc]!
    vld3.8      {d29,d30,d31},[pSrc]!

    vmull.u8    q8,d20,d0
    vmlal.u8    q8,d1
    vmlal.u8    q8,d22,d2
    vmull.u8    q9,d23,d0
    vmlal.u8    q9,d1
    vmlal.u8    q9,d25,d2
    vmull.u8    q10,d26,d0
    vmlal.u8    q10,d1
    vmlal.u8    q10,d28,d2
    vmull.u8    q11,d29,d0
    vmlal.u8    q11,d1
    vmlal.u8    q11,d31,d2

    vrshrn.u16  d24,q8,#8
    vrshrn.u16  d25,q9,#8
    vrshrn.u16  d26,q10,#8
    vrshrn.u16  d27,q11,#8

    subs    size,#64*3]
    pld     [pSrc,#64*4]

    vst1.8      {q12,q13},[pDst]!
    bpl     1b

    cmp     size,#-32
    add     pSrc,pSrc,size
    bxle    lr
    add     pSrc,lsl #1
    add     pDst,pDst,size
    b       1b

    .endfunc
    .end

正如你所看到的,在组装中编写NEON代码比在内在函数中更容易和更短,尽管重度展开.

玩的开心.

c – 使用cvtColor转换单一颜色

c – 使用cvtColor转换单一颜色

我有一种颜色,我想转换为不同的颜色空间.是否有可能直接在cv :: Vec3f上使用cvtColor而不创建1×1 cv :: Mat并使用cv :: Mat上的cvtColor填充该像素,然后从输出中获取唯一的像素?我尝试了以下,但它似乎不喜欢传递一个向量.

有什么建议?

#include <iostream>

#include <opencv2/opencv.hpp>

int main(int,char*[])
{
    cv::Vec3f hsv;
    hsv[0] = .9;
    hsv[1] = .8;
    hsv[2] = .7;

    std::cout << "HSV: " << hsv << std::endl;

    cv::Vec3b bgr;
    cvtColor(hsv,bgr,CV_HSV2BGR); // OpenCV Error: Assertion Failed (scn == 3 && (dcn == 3 || dcn == 4) && (depth == CV_8U || depth == CV_32F)) in cvtColor

    std::cout << "BGR: " << bgr << std::endl; 

    return EXIT_SUCCESS;
}

我也尝试了这个,但得到了一个不同的错误:

#include <iostream>

#include <opencv2/opencv.hpp>

int main(int,char*[])
{
    cv::Mat_<cv::Vec3f> hsv(cv::Vec3f(0.7,0.7,0.8));

    std::cout << "HSV: " << hsv << std::endl;

    cv::Mat_<cv::Vec3b> bgr;

    cvtColor(hsv,CV_HSV2BGR); // OpenCV Error: Assertion Failed (!fixedType() || ((Mat*)obj)->type() == mtype) in create

    std::cout << "BGR: " << bgr << std::endl;

    return EXIT_SUCCESS;
}

解决方法

你的第二种方法是正确的,但你在cvtColor中有不同类型的源和目标,这会导致错误.

请确保同时具有相同类型的hsv和bgr,CV_32F:

#include <opencv2/opencv.hpp>
#include <iostream>

int main()
{
    cv::Mat3f hsv(cv::Vec3f(0.7,0.8));

    std::cout << "HSV: " << hsv << std::endl;

    cv::Mat3f bgr;
    cvtColor(hsv,CV_HSV2BGR); 

    std::cout << "BGR: " << bgr << std::endl;

    return 0;
}

您可以使用Mat3f简洁.它只是一个typedef:

typedef Mat_<Vec3f> Mat3f;

我们今天的关于错误: (-215:Assertion failed) !_src.empty() 在函数 'cv::cvtColor' 中但没有使用 cvt.COLOR()的分享就到这里,谢谢您的阅读,如果想了解更多关于!_src.empty() 函数 'cv::cvtColor' imgprocsrccolor.cpp:182、Android 中使用 OpenCV3.3.0 进行图片比较报错。OpenCV Error:Assertion failed、c – 使用ARM NEON内在函数的cvtColor的SIMD优化、c – 使用cvtColor转换单一颜色的相关信息,可以在本站进行搜索。

本文标签: