C++ 如何在二值图像中恢复形状以进行索引(泛光填充)?
我创建了一个带有阈值的二值图像。如何更改下图中白色形状的颜色以使其可转位 这是我目前的代码:C++ 如何在二值图像中恢复形状以进行索引(泛光填充)?,c++,opencv,image-processing,C++,Opencv,Image Processing,我创建了一个带有阈值的二值图像。如何更改下图中白色形状的颜色以使其可转位 这是我目前的代码: void threshold() { cv::Mat src_8uc3_img = cv::imread("images/train.png", CV_LOAD_IMAGE_GRAYSCALE); // load color image from file system to Mat variable, this will be loaded using 8 bits (uchar)
void threshold()
{
cv::Mat src_8uc3_img = cv::imread("images/train.png", CV_LOAD_IMAGE_GRAYSCALE); // load color image from file system to Mat variable, this will be loaded using 8 bits (uchar)
if (src_8uc3_img.empty()) {
printf("Unable to read input file (%s, %d).", __FILE__, __LINE__);
}
double thresh = 0;
double maxValue = 255;
cv::Mat thresh_holding = src_8uc3_img.clone();
cv::Mat indexing = src_8uc3_img.clone();
cv::imshow("Train", src_8uc3_img);
for (int y = 0; y < thresh_holding.rows ; y++) {
for (int x = 0; x < thresh_holding.cols ; x++) {
uchar thX = thresh_holding.at<uchar>(y, x);
if (thX < 128 ) {
thresh_holding.at<uchar>(y, x) = thresh;
}
else if (thX>128){
thresh_holding.at<uchar>(y, x) = maxValue;
}
}
}
cv::imshow("ThreshHolding", thresh_holding);
cv::waitKey(0); // wait until keypressed
}
第一件事:这是脱粒,不是脱粒。这意味着设置/应用阈值,而不是保持阈值,不管它意味着什么 你想要的是清楚地找到你的形象。除非您想学习基本的图像处理,否则首先要使用该函数。然后不要使用findContours/drawContours,因为它们很慢。如果您想要不同的连接组件,请使用:它很快,而且每个组件都有不同的标签。根据索引,颜色由您决定
Alex Alex Python的启发,这里你可以找到C++版本:
#include <opencv2/opencv.hpp>
int main(void)
{
using namespace cv;
// Read image
Mat1b img = imread("input.png", IMREAD_GRAYSCALE);
// Make sure it's binary
threshold(img, img, 128, 255, THRESH_BINARY);
// Extract connected components
Mat1i labels;
int nlabels = connectedComponents(img, labels);
// Make the connected components from 0 to 255 (assume less than 256 labels)
img = labels * 255 / nlabels;
// Make the labels colored
Mat3b colored, output;
applyColorMap(img, colored, COLORMAP_JET);
// Mask background with zeros in original image
colored.copyTo(output, img);
// Write output
imwrite("output.png", output);
}
唯一的区别是我为图像指定了类型,以便将img的赋值也转换为8bpp。而且打字的垫子使用起来更方便。首先要做的事情是:它是阈值,而不是阈值保持。这意味着设置/应用阈值,而不是保持阈值,不管它意味着什么 你想要的是清楚地找到你的形象。除非您想学习基本的图像处理,否则首先要使用该函数。然后不要使用findContours/drawContours,因为它们很慢。如果您想要不同的连接组件,请使用:它很快,而且每个组件都有不同的标签。根据索引,颜色由您决定
Alex Alex Python的启发,这里你可以找到C++版本:
#include <opencv2/opencv.hpp>
int main(void)
{
using namespace cv;
// Read image
Mat1b img = imread("input.png", IMREAD_GRAYSCALE);
// Make sure it's binary
threshold(img, img, 128, 255, THRESH_BINARY);
// Extract connected components
Mat1i labels;
int nlabels = connectedComponents(img, labels);
// Make the connected components from 0 to 255 (assume less than 256 labels)
img = labels * 255 / nlabels;
// Make the labels colored
Mat3b colored, output;
applyColorMap(img, colored, COLORMAP_JET);
// Mask background with zeros in original image
colored.copyTo(output, img);
// Write output
imwrite("output.png", output);
}
唯一的区别是我为图像指定了类型,以便将img的赋值也转换为8bpp。而且键入的Mats使用起来更方便。Python代码:
import numpy as np
import cv2
a=cv2.imread('P.png')
b=cv2.cvtColor(a, cv2.COLOR_BGR2GRAY)
b=cv2.threshold(b, 170, 255, cv2.THRESH_BINARY)[1]
lbl=cv2.connectedComponentsWithStats(b, 8, cv2.CV_32S)
im_lbl=np.uint8(255*lbl[1]/lbl[0])
out_img=cv2.applyColorMap(im_lbl, cv2.COLORMAP_JET)
out_img=cv2.bitwise_and(out_img, a)
cv2.imwrite('out.png', out_img)
结果:
Python代码:
import numpy as np
import cv2
a=cv2.imread('P.png')
b=cv2.cvtColor(a, cv2.COLOR_BGR2GRAY)
b=cv2.threshold(b, 170, 255, cv2.THRESH_BINARY)[1]
lbl=cv2.connectedComponentsWithStats(b, 8, cv2.CV_32S)
im_lbl=np.uint8(255*lbl[1]/lbl[0])
out_img=cv2.applyColorMap(im_lbl, cv2.COLORMAP_JET)
out_img=cv2.bitwise_and(out_img, a)
cv2.imwrite('out.png', out_img)
结果:
你的问题是什么?我有一张只有白色形状的图片,我需要将它们的颜色更改为我想要的颜色。@Akgali检查答案,现在还包括exampe代码。如果它解决了问题,请接受。你的问题是什么?我有一张只有白色形状的图片,我需要将它们的颜色更改为我想要的颜色。@Akgali检查答案,现在还包括exampe代码。如果它解决了问题,就接受它。