找到镂空图片的坐标

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BuildLogoImageCoord
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void btnText_Click(object sender, EventArgs e)
        {
            FindCoords(BuildTextImage(txtText.Text), txtText.Text);
            MessageBox.Show("Done!");
        }

        const int ImgWidth = 1920;
        const int ImgHeight = 1200;
        static Bitmap BuildTextImage(string text)
        {
            Bitmap bitmap = new Bitmap(ImgWidth, ImgHeight);
            Graphics g = Graphics.FromImage(bitmap);
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
            g.FillRectangle(new SolidBrush(Color.White), 0, 0, bitmap.Width, bitmap.Height);
            using (Font font1 = new Font("Arial", 1200, FontStyle.Bold, GraphicsUnit.Pixel))
            {
                Rectangle rect1 = new Rectangle(0, 100, ImgWidth, ImgHeight);
                StringFormat stringFormat = new StringFormat();
                stringFormat.Alignment = StringAlignment.Center;
                stringFormat.LineAlignment = StringAlignment.Center;
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

                Font goodFont = FindGoodFont(g, text, rect1.Size, font1, GraphicsUnit.Pixel);
                g.DrawString(text, goodFont, Brushes.Black, rect1, stringFormat);
            }
            g.Dispose();
            bitmap.Save(text + ".jpg", ImageFormat.Jpeg);
            return bitmap;
        }

        private static Font FindGoodFont(Graphics Graf, string sStringToFit,
                                    Size TextRoomAvail,
                                    Font FontToUse,
                                    GraphicsUnit FontUnit)
        {
            // Find out what the current size of the string in this font is
            SizeF RealSize = Graf.MeasureString(sStringToFit, FontToUse);
            if ((RealSize.Width <= TextRoomAvail.Width) && (RealSize.Height <= TextRoomAvail.Height))
            {
                // The current font is fine...
                return FontToUse;
            }

            // Either width or height is too big...
            // Usually either the height ratio or the width ratio
            // will be less than 1. Work them out...
            float HeightScaleRatio = TextRoomAvail.Height / RealSize.Height;
            float WidthScaleRatio = TextRoomAvail.Width / RealSize.Width;

            // We'll scale the font by the one which is furthest out of range...
            float ScaleRatio = (HeightScaleRatio < WidthScaleRatio) ? ScaleRatio = HeightScaleRatio : ScaleRatio = WidthScaleRatio;
            float ScaleFontSize = FontToUse.Size * ScaleRatio;

            // Retain whatever the style was in the old font...
            FontStyle OldFontStyle = FontToUse.Style;

            // Get rid of the old non working font...
            FontToUse.Dispose();

            // Tell the caller to use this newer smaller font.
            FontToUse = new Font(FontToUse.FontFamily,
                                    ScaleFontSize,
                                    OldFontStyle,
                                    FontUnit);
            return FontToUse;
        }

        private const int StartSize = 30;

        static void FindCoords(Bitmap bitmap, string text)
        {
            Bitmap reff = new Bitmap(ImgWidth, ImgHeight);
            Graphics g = Graphics.FromImage(reff);
            g.DrawImage(Image.FromFile("bgImage.jpg"), new Rectangle(0, 0, ImgWidth, ImgHeight));

            var rows = ImgHeight/StartSize;
            var cols = ImgWidth/StartSize;
            var coords = new List<string>();
            for (int j = 0; j < rows; j++)
            {
                for (int i = 0; i < cols; i++)
                {
                    var total = StartSize*StartSize;
                    var cur = 0;
                    for (int ii = 0; ii < StartSize; ii++)
                    {
                        for (int jj = 0; jj < StartSize; jj++)
                        {
                            var posx = i*StartSize + ii;
                            var posy = j*StartSize + jj;
                            byte color = bitmap.GetPixel(posx, posy).R;
                            if (color == 0)
                            {
                                cur++;
                                reff.SetPixel(posx, posy, Color.Transparent);
                            }
                        }
                    }
                    if (cur > total * 0.02)
                    {
                        coords.Add("[" + i + "," + j + "]");
                    }
                }
            }

            StreamWriter sw = new StreamWriter(text + ".txt");
            sw.Write("[" + string.Join(",", coords.ToArray()) + "]");
            sw.Close();

            g.Dispose();
            reff.Save(text + "_cover.png", ImageFormat.Png);
        }
    }
}

  

转载于:https://www.cnblogs.com/sunnie/p/5718179.html

要识别镂空面并打印其坐标,您可以使用Python中的ogr库来读取shp文件并进行处理。以下是一个示例代码: ```python from osgeo import ogr # 打开shp文件 shp_file = "path/to/your/shp/file.shp" driver = ogr.GetDriverByName("ESRI Shapefile") data_source = driver.Open(shp_file, 0) # 获取第一个图层 layer = data_source.GetLayer(0) # 遍历要素 for feature in layer: geometry = feature.GetGeometryRef() # 判断是否为镂空面 if geometry.GetGeometryName() == "POLYGON": ring = geometry.GetGeometryRef(0) if ring.IsClosed() and ring.GetPointCount() > 3: # 判断是否有内环 if geometry.GetGeometryCount() > 1: print("Found a polygon with holes:", feature.GetFID()) # 获取外环的坐标 exterior_ring = geometry.GetGeometryRef(0) exterior_coordinates = [] for i in range(exterior_ring.GetPointCount()): point = exterior_ring.GetPoint(i) exterior_coordinates.append((point[0], point[1])) print("Exterior Coordinates:", exterior_coordinates) # 获取内环的坐标 for i in range(1, geometry.GetGeometryCount()): interior_ring = geometry.GetGeometryRef(i) interior_coordinates = [] for j in range(interior_ring.GetPointCount()): point = interior_ring.GetPoint(j) interior_coordinates.append((point[0], point[1])) print("Interior Coordinates:", interior_coordinates) # 关闭数据源 data_source = None ``` 在上述代码中,我们首先打开shp文件并获取第一个图层。然后,遍历图层中的要素,判断每个要素的几何类型是否为多边形(即镂空面)。如果满足条件,则打印出找到镂空面的要素ID。接下来,我们获取外环(多边形的边界)的坐标,并将其打印出来。然后,通过循环获取内环(镂空部分)的坐标,并将其分别打印出来。 请确保将代码中的`"path/to/your/shp/file.shp"`替换为实际的shp文件路径。 希望这可以满足您的需求!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值