尝试使用 OpenCV 更改小 png 图像的颜色 (Java)
Trying to change color of a small png image using OpenCV (Java)
这里我使用带有 java 的 OpenCV lib 将透明部分更改为白色,并将其内部的形状更改为黑色和稍厚。我尝试使用 cvtColor(img, hsv, Imgproc.COLOR_BGR2GRAY);但整个图像变成灰色。我需要这方面的帮助
这是我需要更改颜色的原始图像
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String img_url1 = "C:\\Users\\me\\Desktop\\cpt\\1.png";
Mat img = Imgcodecs.imread(img_url1);
if( img.empty() ) {
System.out.println("Error opening image!");
System.out.println("Program Arguments: [image_name -- default ../data/lena.jpg] \n");
System.exit(-1);
}
Mat hsv = new Mat();
Imgproc.cvtColor(img, hsv, Imgproc.COLOR_BGR2GRAY);
Imgcodecs.imwrite("C:\\Users\\me\\Desktop\\cpt\\1-cpy.png", hsv);
处理后的输出图像:
这是一个 C++ 代码,但您可以轻松地将其转换为 JAVA。
Mat img = imread("image.png",-1);
//split channels, extract 3rd channel
std::vector<Mat> channels;
split(img, channels);
// convert to white background and black foreground
Mat black;
bitwise_not(channels.at(3), black);
imshow("image", black);
waitKey(0);
(1) Read the PNG
with Alpha channel
with the flag IMREAD_UNCHANGED
.
(2) Then Split the channels and get the alpha.
(3) Other steps ...
import java.util.*;
import org.opencv.core.*;
import org.opencv.imgproc.Imgproc;
import org.opencv.imgcodecs.Imgcodecs;
public class xtmp{
public static void main(String[] args){
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
test();
}
static void test(){
// Read with alpha channel
Mat img = Imgcodecs.imread("transparent.png", Imgcodecs.IMREAD_UNCHANGED);
// Split the channels and get the alpha
List<Mat> bgra = new ArrayList<Mat>(4);
Core.split(img, bgra) ;
// Save
Mat alpha = bgra.get(3);
Imgcodecs.imwrite("alpha.png", alpha);
}
}
透明:
阿尔法:
我刚刚在@zindarod 的回答的帮助下弄明白了,这是解决方案
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
String img_url1 = "C:\\Users\\me\\Desktop\\cpt\\1.png";
Mat img = Imgcodecs.imread(img_url1, -1);
List<Mat> channels = new ArrayList<>();
Core.split(img, channels);
Mat black = new Mat();
Core.bitwise_not(channels.get(3), black);
String file2 = "C:\\\\Users\\\\me\\\\Desktop\\\\cpt\\\\1-cpy.png";
Imgcodecs.imwrite(file2, black);
这里我使用带有 java 的 OpenCV lib 将透明部分更改为白色,并将其内部的形状更改为黑色和稍厚。我尝试使用 cvtColor(img, hsv, Imgproc.COLOR_BGR2GRAY);但整个图像变成灰色。我需要这方面的帮助
这是我需要更改颜色的原始图像
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String img_url1 = "C:\\Users\\me\\Desktop\\cpt\\1.png";
Mat img = Imgcodecs.imread(img_url1);
if( img.empty() ) {
System.out.println("Error opening image!");
System.out.println("Program Arguments: [image_name -- default ../data/lena.jpg] \n");
System.exit(-1);
}
Mat hsv = new Mat();
Imgproc.cvtColor(img, hsv, Imgproc.COLOR_BGR2GRAY);
Imgcodecs.imwrite("C:\\Users\\me\\Desktop\\cpt\\1-cpy.png", hsv);
处理后的输出图像:
这是一个 C++ 代码,但您可以轻松地将其转换为 JAVA。
Mat img = imread("image.png",-1);
//split channels, extract 3rd channel
std::vector<Mat> channels;
split(img, channels);
// convert to white background and black foreground
Mat black;
bitwise_not(channels.at(3), black);
imshow("image", black);
waitKey(0);
(1) Read the
PNG
withAlpha channel
with the flagIMREAD_UNCHANGED
.(2) Then Split the channels and get the alpha.
(3) Other steps ...
import java.util.*;
import org.opencv.core.*;
import org.opencv.imgproc.Imgproc;
import org.opencv.imgcodecs.Imgcodecs;
public class xtmp{
public static void main(String[] args){
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
test();
}
static void test(){
// Read with alpha channel
Mat img = Imgcodecs.imread("transparent.png", Imgcodecs.IMREAD_UNCHANGED);
// Split the channels and get the alpha
List<Mat> bgra = new ArrayList<Mat>(4);
Core.split(img, bgra) ;
// Save
Mat alpha = bgra.get(3);
Imgcodecs.imwrite("alpha.png", alpha);
}
}
透明:
阿尔法:
我刚刚在@zindarod 的回答的帮助下弄明白了,这是解决方案
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
String img_url1 = "C:\\Users\\me\\Desktop\\cpt\\1.png";
Mat img = Imgcodecs.imread(img_url1, -1);
List<Mat> channels = new ArrayList<>();
Core.split(img, channels);
Mat black = new Mat();
Core.bitwise_not(channels.get(3), black);
String file2 = "C:\\\\Users\\\\me\\\\Desktop\\\\cpt\\\\1-cpy.png";
Imgcodecs.imwrite(file2, black);