|
发表于 2024-12-7 11:22:09
|
显示全部楼层
To achieve a halftone effect with specific angles for the dot grid, like 15°, 45°, 75°, or 90°, you need to consider how the grid of dots is laid out across the image. This involves setting a pattern where dots are repeated in a grid that is rotated by the given angle.
Here is a method using OpenCV to create a halftone effect, where the dots are aligned with a specified grid angle. This involves creating a grid based on the specified line per inch (LPI) and using that to modulate the intensity of the zones of the image.
#include <opencv2/opencv.hpp>
#include <iostream>
#include <cmath>
using namespace cv;
// Function to generate a halftone pattern
void createHalftonePattern(Mat &pattern, int dotSpacing, double angle) {
int size = dotSpacing * 2; // Size of the pattern
pattern = Mat(size, size, CV_8UC1, Scalar(0));
// Convert angle to radians
double rad = angle * CV_PI / 180.0;
// Calculate rotation transformation matrix
Point center(size / 2, size / 2);
Mat rotationMatrix = getRotationMatrix2D(center, angle, 1);
// Create a temporary pattern with a circle
Mat tempPattern = Mat::zeros(size, size, CV_8UC1);
circle(tempPattern, center, dotSpacing / 2, Scalar(255), FILLED); // Fill a circle
// Apply the rotation to the pattern
warpAffine(tempPattern, pattern, rotationMatrix, pattern.size());
}
// Apply the halftone pattern to an image
void applyHalftonePattern(const Mat &src, Mat &dst, int dotSpacing, double angle) {
Mat pattern;
createHalftonePattern(pattern, dotSpacing, angle);
dst = src.clone();
for (int y = 0; y < src.rows; y += dotSpacing) {
for (int x = 0; x < src.cols; x += dotSpacing) {
Rect roi(x, y, dotSpacing, dotSpacing);
if (x + dotSpacing <= src.cols && y + dotSpacing <= src.rows) {
Mat srcRoi = src(roi);
Mat dstRoi = dst(roi);
double meanIntensity = mean(srcRoi)[0];
// Modulate dot size based on mean intensity of the block
double radius = dotSpacing * (1.0 - meanIntensity / 255.0) / 2.0;
// Draw dots according to the mean intensity
Point center(dotSpacing / 2, dotSpacing / 2);
Mat mask = Mat::zeros(srcRoi.size(), srcRoi.type());
circle(mask, center, static_cast<int>(radius), Scalar(255), -1);
bitwise_and(pattern, mask, dstRoi);
}
}
}
}
int main() {
Mat img = imread("your_image_path.jpg", IMREAD_GRAYSCALE);
if (img.empty()) {
std::cerr << "Error: Cannot load image" << std::endl;
return -1;
}
Mat halftoneImg;
int dotSpacing = 20; // Set LPI by controlling the spacing
double angle = 45.0; // Angle of the dot grid
applyHalftonePattern(img, halftoneImg, dotSpacing, angle);
imshow("Halftone Image", halftoneImg);
waitKey(0);
return 0;
}
**Key Points:**
- **Pattern Rotation**: This code first creates a pattern with circles, then uses an affine transformation matrix to rotate the entire pattern to the specified angle.
- **Dot Arrangement**: Dots are applied to the image grid, taking into account the mean intensity of the underlying pixels to modulate the dot size.
- **Angle Control**: The `angle` variable controls the overall orientation of the dot grid across the image, determining the visual direction at which the halftone is perceived.
Please adjust the `dotSpacing` and `angle` to experiment with different patterns and densities. This code aims to simulate a classic halftone effect with flexible grid angles.
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|