package main import ( "flag" "image" "image/color" "image/jpeg" "log" "os" ) //…
- User
package main
import ( "flag" "image" "image/color" "image/jpeg" "log" "os" )
// ConvertToGrayScale преобразует изображение в градации серого func ConvertToGrayScale(img image.Image) image.Gray { bounds := img.Bounds() gray := image.NewGray(bounds) for y := bounds.Min.Y; y < bounds.Max.Y; y++ { for x := bounds.Min.X; x < bounds.Max.X; x++ { r, g, b, _ := img.At(x, y).RGBA() // Вычисляем яркость с помощью стандартной формулы для RGB в градациях серого grey := uint8((r299 + g587 + b114 + 500) / 1000 >> 8) gray.SetGray(x, y, color.Gray{Y: grey}) } } return gray }
// DetectEdges выполняет детектирование контуров с использованием оператора Собела func DetectEdges(gray *image.Gray, threshold uint8) *image.Gray { bounds := gray.Bounds() edges := image.NewGray(bounds)
sobelX := [3][3]int{ {-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}, } sobelY := [3][3]int{ {-1, -2, -1}, {0, 0, 0}, {1, 2, 1}, } for y := 1; y < bounds.Max.Y-1; y++ { for x := 1; x < bounds.Max.X-1; x++ { var gx, gy int for ky := -1; ky <= 1; ky++ { for kx := -1; kx <= 1; kx++ { c := gray.GrayAt(x+kx, y+ky) gx += int(c.Y) * sobelX[ky+1][kx+1] gy += int(c.Y) * sobelY[ky+1][kx+1] } } // Вычисление градиента g := uint8(clamp(abs(gx)+abs(gy), 0, 255)) // Применение порога if g >= threshold { edges.SetGray(x, y, color.Gray{Y: 255}) } else { edges.SetGray(x, y, color.Gray{Y: 0}) } } } return edges}
// clamp ограничивает значение в пределах от min до max func clamp(value, min, max int) int { if value < min { return min } else if value > max { return max } return value }
// abs возвращает абсолютное значение целого числа func abs(x int) int { if x < 0 { return -x } return x }
func main() { // Чтение аргументов командной строки inputPath := flag.String("input", "input.jpg", "Path to input image") outputPath := flag.String("output", "output.jpg", "Path to save output image") threshold := flag.Uint("threshold", 128, "Threshold for edge detection") flag.Parse()
// Открытие изображения file, err := os.Open(*inputPath) if err != nil { log.Fatalf("Failed to open: %s", err) } defer file.Close() img, err := jpeg.Decode(file) if err != nil { log.Fatalf("Failed to decode: %s", err) } // Преобразование в градации серого gray := ConvertToGrayScale(img) // Выделение контуров edges := DetectEdges(gray, uint8(*threshold)) // Сохраняем изображение с контурами outFile, err := os.Create(*outputPath) if err != nil { log.Fatalf("Failed to create: %s", err) } defer outFile.Close() if err := jpeg.Encode(outFile, edges, nil); err != nil { log.Fatalf("Failed to encode: %s", err) } log.Printf("Edges detected and saved as '%s'\n", *outputPath)}
добавь в этот скрипт развознавание людей испольузй gorgonia испольузй именно gorgonia
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.