Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image processing 如何用一种纯色填充OpenCV图像?_Image Processing_Opencv - Fatal编程技术网

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

Image processing 如何用一种纯色填充OpenCV图像?,image-processing,opencv,Image Processing,Opencv,如何用一种纯色填充OpenCV图像?使用OpenCV C API和IplImage*img: 使用:cvSet(img,CV_RGB(redVal,greenVal,blueVal)) 使用OpenCV C++ API,代码

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

使用OpenCV C API和
IplImage*img

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

使用OpenCV C++ API,代码,则使用: 例如:

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


最简单的方法是使用OpenCV Mat类:

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

其中,
img
被定义为
cv::Mat

以下是如何在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)
对于8位(CV_8U)OpenCV图像,语法为:

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

如果您使用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

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

注:

  • 先高后宽
  • CV_8UC3型表示8位无符号整数,3个通道
  • 颜色格式为BGR
    • 使用。这是一个Python,它创建了一个灰色、蓝色、绿色和红色的图像,并以2x2的网格显示

      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')
      

      我个人编写了这段python代码,用于更改使用openCV打开或创建的整个图像的颜色。如果不够好,我很抱歉,我是初学者,应该是红色、绿色、蓝色还是蓝色、绿色、红色?opencv中的不是蓝、绿、红吗?是蓝、绿、红。事实上,OpenCV是通道顺序不可知的。您是正确的,最常见的通道顺序(在PC上)是
      BGR
      ,尽管这与此答案不太相关。通常,用户有责任跟踪频道顺序。@AdiShavit我们可以从图片中删除设置的颜色吗?@ShwetabhShekhar:这个问题有很多不同的含义和答案,远远超出了评论的范围。。。试着把它作为一个完整的问题来提问,并为你想做的事情提供更多的背景。但简单地回答,从现有的C数组中“删除”三元组值是什么意思?如何使用非灰色进行此操作?使用非灰色示例更新。@orangepips,这是一个很好的答案。顺便问一下,你能告诉我android(opencv)中是否有函数或操作符用于python中的[:,0]=1之类的东西吗?它也用于您发布的python代码。欢迎使用SO!当你要回答一个已经有公认答案的老问题(这个问题已经有将近10年的历史了)(这里就是这种情况)时,请扪心自问:我真的有实质性的改进吗?如果没有,请考虑不要回答。代码转储不能得到好的答案。您应该解释这是如何以及为什么解决他们的问题的。我建议阅读,@Amir Dora完全不同意。这是迄今为止最好的答案,以最清晰、简洁和直接的方式回答了这个问题。
      color=(200, 100, 255) # sample of a color 
      img = np.full((100, 100, 3), color, np.uint8)
      
      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')
      
      color=(200, 100, 255) # sample of a color 
      img = np.full((100, 100, 3), color, np.uint8)