【问题标题】:How to fill OpenCV image with one solid color?如何用一种纯色填充 OpenCV 图像?
【发布时间】:2011-05-19 06:48:28
【问题描述】:

如何用一种纯色填充 OpenCV 图像?

【问题讨论】:

标签: image-processing opencv


【解决方案1】:

将 OpenCV C API 与 IplImage* img 一起使用:

使用cvSet():cvSet(img, CV_RGB(redVal,greenVal,blueVal));

将 OpenCV C++ API 与 cv::Mat img 一起使用,然后使用:

cv::Mat::operator=(const Scalar& s) 如:

img = cv::Scalar(redVal,greenVal,blueVal);

the more general, mask supporting, cv::Mat::setTo()

img.setTo(cv::Scalar(redVal,greenVal,blueVal));

【讨论】:

  • 应该是红、绿、蓝还是蓝、绿、红? opencv中不是蓝绿红吗?
  • 它是蓝色、绿色、红色。确认
  • 实际上,OpenCV 与通道顺序无关。最常见的频道顺序(在 PC 上)是 BGR 是正确的,尽管这与此答案不太相关。跟踪频道顺序通常是用户的责任。
  • @AdiShavit 我们可以从图片中删除设置的颜色吗?
  • @ShwetabhShekhar:这个问题有很多不同的含义和答案,以至于超出了评论的范围...尝试将其作为一个完整的问题提出,并为您想做的事情提供更多背景信息.但简单地回答,您从现有 C 数组中“删除”三个值是什么意思?
【解决方案2】:

下面是如何在 Python 中使用 cv2:

# Create a blank 300x300 black image
image = np.zeros((300, 300, 3), np.uint8)
# Fill image with red color(set each pixel to red)
image[:] = (0, 0, 255)

这里有一个更完整的例子,如何创建一个新的填充了某种 RGB 颜色的空白图像

import cv2
import numpy as np

def create_blank(width, height, rgb_color=(0, 0, 0)):
    """Create new image(numpy array) filled with certain color in RGB"""
    # Create black blank image
    image = np.zeros((height, width, 3), np.uint8)

    # Since OpenCV uses BGR, convert the color first
    color = tuple(reversed(rgb_color))
    # Fill image with color
    image[:] = color

    return image

# Create new blank 300x300 red image
width, height = 300, 300

red = (255, 0, 0)
image = create_blank(width, height, rgb_color=red)
cv2.imwrite('red.jpg', image)

【讨论】:

    【解决方案3】:

    最简单的是使用 OpenCV Mat 类:

    img=cv::Scalar(blue_value, green_value, red_value);
    

    其中img 被定义为cv::Mat

    【讨论】:

      【解决方案4】:

      创建一个新的 640x480 图像并用紫色(红色+蓝色)填充它:

      cv::Mat mat(480, 640, CV_8UC3, cv::Scalar(255,0,255));
      

      注意:

      • 先高后宽
      • 类型 CV_8UC3 表示 8 位无符号整数,3 个通道
      • 颜色格式为 BGR

      【讨论】:

        【解决方案5】:

        使用numpy.full。这是一个创建灰色、蓝色、绿色和红色图像并以 2x2 网格显示的 Python。

        import cv2
        import numpy as np
        
        gray_img = np.full((100, 100, 3), 127, np.uint8)
        
        blue_img = np.full((100, 100, 3), 0, np.uint8)
        green_img = np.full((100, 100, 3), 0, np.uint8)
        red_img = np.full((100, 100, 3), 0, np.uint8)
        
        full_layer = np.full((100, 100), 255, np.uint8)
        
        # OpenCV goes in blue, green, red order
        blue_img[:, :, 0] = full_layer
        green_img[:, :, 1] = full_layer
        red_img[:, :, 2] = full_layer
        
        cv2.imshow('2x2_grid', np.vstack([
            np.hstack([gray_img, blue_img]), 
            np.hstack([green_img, red_img])
        ]))
        cv2.waitKey(0)
        cv2.destroyWindow('2x2_grid')
        

        【讨论】:

        • 你会如何用非灰色来做这个?
        • 更新为非灰色示例。
        • @orangepips,这是一个很好的答案。顺便说一句,你能告诉我在 android (opencv) 中是否有任何函数或运算符,比如 python 中的 [:,0]=1 ?它也用于您发布的 python 代码。
        【解决方案6】:

        对于 8 位 (CV_8U) OpenCV 图像,语法为:

        Mat img(Mat(nHeight, nWidth, CV_8U);
        img = cv::Scalar(50);    // or the desired uint8_t value from 0-255
        

        【讨论】:

          【解决方案7】:
          color=(200, 100, 255) # sample of a color 
          img = np.full((100, 100, 3), color, np.uint8)
          

          【讨论】:

          • @Amir Dora 完全不同意。这是迄今为止最好的答案,以最清晰、最简洁、最直接的方式回答问题。
          • @AmirDora,我也完全不同意。答案简明扼要,准确回答问题。
          • 这是一个社区指南,最好遵循。但我已经删除了我的评论。
          【解决方案8】:

          如果你使用 Java for OpenCV,那么你可以使用下面的代码。

          Mat img = src.clone(); //Clone from the original image
          img.setTo(new Scalar(255,255,255)); //This sets the whole image to white, it is R,G,B value
          

          【讨论】:

            【解决方案9】:

            我亲自编写了这个 python 代码来更改使用 openCV 打开或创建的整个图像的颜色。不够好见谅,我是初学者??。

            def OpenCvImgColorChanger(img,blue = 0,green = 0,red = 0):
            line = 1
            ImgColumn = int(img.shape[0])-2
            ImgRaw  = int(img.shape[1])-2
            
            
            
            for j in range(ImgColumn):
            
                for i in range(ImgRaw):
            
                    if i == ImgRaw-1:
                        line +=1
            
                    img[line][i][2] = int(red)
                    img[line][i][1] = int(green)
                    img[line][i][0] = int(blue)
            

            【讨论】:

            • 欢迎来到 SO!当你要回答一个已经有公认答案的老问题(这个问题差不多 10 年了)时(这里就是这种情况),请问问自己:我真的有实质性的改进吗?如果没有,请考虑不要回答。
            猜你喜欢
            • 2010-12-15
            • 2012-06-22
            • 1970-01-01
            • 2013-04-23
            • 2019-12-11
            • 1970-01-01
            • 2013-01-12
            • 1970-01-01
            • 2017-05-28
            相关资源
            最近更新 更多