Image Segmentation
So what does KMeans Clustering have to do with images? Well, it can find clusters of different colours for you resulting in image segmentation. The data in image is the pixel colours. We need to provide the algorithm the number of clusters. This is relatively easy as we can guess the number of distinct colours by looking at the image. This does not have to be exact but depending on the number of clusters, we may or may not get good segmentation. In the above image, you can see that I have used 3 clusters which means the image is segmented in 3 distinct colours. Okay now based on the number of clusters, the algorithm is going to group similar colours together till it cannot find any colours to move around the clusters. So let’s have a look at the algorithm next.
KMeans Algorithm
Here is how KMeans Algorithm learns data clusters:
Set number of clusters.
Set or randomly set the centroids of these clusters.
For all data:
Find the distance between centroids of the clusters and the data.
Move the data to the cluster that has the least distance to it.
Calculate the new centroids of the clusters now that its data may have moved.
If the new centroids are different from old centroids, then go to step 3, otherwise stop.
And this is pretty much it! So let’s understand this: we know what steps 1 & 2 mean. For step 2, I get the top X colours from the image and use its values as centroid. In other words, if I said I want 2 clusters, then if top 2 colours (colours with most pixels) from the image are RGB:100,200,130 & 40,70,140, then these values become my starting 2 centroids.
Step 3a is where we would read each pixel in the image and find its distance from the 2 centroids. Now what is that distance? I have used Euclidean Distance but you could use any. But remember that type of distance used will affect the segmentation results. This is because using this distance, we measure how similar the current pixel colour is to the cluster centroid. To emphasis this point, I have used RGB and HSV colour models. Using the same number of clusters for RGB & HSV will produce different segmentation results.
Step 3b moves pixels to closest clusters. So if for instance, pixel 110,215,110 will be closest based on Euclidean Distance to first cluster centroid and pixel 50,80,160 will be closest to the second.
Step 4 is where we find new centroids for the clusters. In the current example, the new centroids are: 100+110/2,200+215/2,130+110/2 and 40+50/2,70+80/2,140+160/2. Yes the new centroids are found using average.
Step 5 is convergence check. If the new centroids are the same as the old centroids, then that means the data within the centroids have not moved and the clusters have stabilized!
ภาพเซ็ก
เพื่อ อะไรไม่ KMeans คลัสเตอร์ต้อง มีภาพหรือไม่ ดี มันสามารถหาของสีที่เกิดขึ้นในการแบ่งรูป ในรูปเป็นสีพิกเซล เราจำเป็นต้องมีขั้นตอนวิธีการของคลัสเตอร์ นี้เป็นเรื่องง่ายเราสามารถคาดเดาจำนวนของสีที่แตกต่างกัน โดยดูที่รูป ไม่จำเป็นแน่นอน แต่จำนวนคลัสเตอร์ เราอาจ หรืออาจไม่ได้รับการแบ่งดี ในรูปด้านบน คุณสามารถดูว่า ฉันได้ใช้คลัสเตอร์ 3 ซึ่งหมายความว่า ภาพจะถูกแบ่งเป็นช่วงใน 3 สีที่แตกต่างกัน เอาล่ะตอนนี้ขึ้นอยู่กับจำนวนของคลัสเตอร์ อัลกอริทึมจะไปกลุ่มสีคล้ายกันจนกว่าจะไม่พบสีใด ๆ เพื่อย้ายไปรอบ ๆ ในคลัสเตอร์ ดังนั้นลองมีดูที่ถัดไปอัลกอริทึม
KMeans อัลกอริทึม
นี่คือวิธีอัลกอริทึม KMeans ทราบคลัสเตอร์ข้อมูล:
ตั้งหมายเลขของคลัสเตอร์
ตั้ง หรือสุ่มตั้ง centroids ของคลัสเตอร์เหล่านี้
สำหรับข้อมูลทั้งหมด:
หาระยะห่างระหว่าง centroids ของคลัสเตอร์และข้อมูลการ
ย้ายข้อมูลไปยังคลัสเตอร์ที่มีระยะห่างน้อยที่สุดจะ
คำนวณ centroids ใหม่ของคลัสเตอร์ที่อาจมีย้ายข้อมูล.
ถ้า centroids ใหม่แตกต่างจากเก่า centroids แล้วไปขั้นตอนที่ 3 อื่น ๆ ต้องการ
และนี่คือมันสวยมาก เข้าใจกันนี้: เรารู้ว่าขั้นตอน 1 & 2 หมายถึงการ สำหรับขั้นตอนที่ 2 ฉันได้รับบน X สีจากภาพ และใช้ค่าของเซนทรอยด์ ในคำอื่น ๆ ถ้าฉันบอกว่า ฉันต้องการคลัสเตอร์ 2 แล้วถ้าบน 2 สี (สี มีพิกเซลมากที่สุด) จากภาพ RGB:100, 200, 130 & 40,70,140 แล้วเหล่านี้ค่ากลายเป็น ของฉันเริ่มต้น 2 centroids.
ขั้นตอน 3a เป็นที่เราจะอ่านแต่ละพิกเซลในภาพ และค้นหาจาก 2 centroids ตอนนี้ คืออะไรจากนั้น ใช้แบบยุคลิด แต่คุณสามารถใช้ใด ๆ แต่จำได้ว่า ชนิดของระยะทางที่ใช้จะมีผลต่อผลลัพธ์แบ่ง ทั้งนี้เนื่องจากใช้ระยะนี้ เราวัดเป็นสีพิกเซลปัจจุบันคล้ายคลึงกับเซนทรอยด์ของคลัสเตอร์ การเน้น จุดนี้ ฉันได้ใช้ RGB และ HSV สีรูปแบบ ใช้หมายเลขเดียวกันของคลัสเตอร์สำหรับ RGB &จะผลิต HSV ผลลัพธ์แตกต่างกันแบ่ง
ขั้นตอน 3b ย้ายเซลไปคลัสเตอร์ที่ใกล้เคียงที่สุด ดังนั้นหากตัวอย่าง เซล 110,215,110 จะใกล้ยึดแบบยุคลิดจะก่อน คลัสเตอร์เซนทรอยด์ และเซล 50,80,160 จะใกล้เคียงกับที่สอง
4 ขั้นตอนคือ ที่เราหาใหม่ centroids ที่คลัสเตอร์ ตัวอย่างปัจจุบัน คือ centroids ใหม่: 100 110/2200 160 80/2,140 215/2130 110/2 และ 40 50/2,70/2 ใช่ centroids ใหม่อยู่ใช้เฉลี่ย
ขั้นตอนที่ 5 ตรวจสอบการบรรจบกันได้ ถ้า centroids ใหม่เหมือนกับ centroids เก่า แล้วนั่นหมายความว่า ยังไม่ได้ย้ายข้อมูลภายใน centroids และคลัสเตอร์การชุมนุม
การแปล กรุณารอสักครู่..
![](//thimg.ilovetranslation.com/pic/loading_3.gif?v=b9814dd30c1d7c59_8619)
การแบ่งส่วนภาพ
ดังนั้นสิ่งที่จะ kmeans Clustering ต้องทำกับภาพที่? ดีก็สามารถหากลุ่มของสีที่แตกต่างกันสำหรับคุณที่เกิดในการแบ่งส่วนภาพ ข้อมูลที่อยู่ในภาพเป็นสีพิกเซล เราจำเป็นต้องมีขั้นตอนวิธีการที่จะให้จำนวนของกลุ่ม นี้เป็นเรื่องง่ายที่เราสามารถคาดเดาจำนวนของสีที่แตกต่างกันโดยดูที่ภาพ นี้ไม่ได้เป็นที่แน่นอน แต่ขึ้นอยู่กับจำนวนของกลุ่มที่เราอาจจะหรืออาจไม่ได้รับการแบ่งส่วนที่ดี ในภาพข้างต้นคุณจะเห็นว่าฉันได้ใช้ 3 กลุ่มซึ่งหมายความว่าภาพจะถูกแบ่งใน 3 สีที่แตกต่าง เอาล่ะตอนนี้ขึ้นอยู่กับจำนวนของกลุ่มอัลกอริทึมที่จะกลุ่มสีที่คล้ายกันเข้าด้วยกันจนไม่สามารถหาสีใด ๆ ที่จะย้ายไปรอบ ๆ กลุ่ม ดังนั้นเรามาดูที่ขั้นตอนวิธีการต่อไปขั้นตอนวิธี kmeans นี่เป็นวิธีการเรียนรู้ขั้นตอนวิธี kmeans กลุ่มข้อมูล: กำหนดจำนวนของกลุ่มที่ตั้งหรือตั้ง centroids ของกลุ่มเหล่านี้สุ่มสำหรับข้อมูลทั้งหมด: หาระยะห่างระหว่าง centroids ของกลุ่มและ ข้อมูลย้ายข้อมูลไปยังกลุ่มที่มีระยะทางที่น้อยไปคำนวณ centroids ใหม่ของกลุ่มในขณะนี้ว่าข้อมูลอาจจะได้ย้ายหาก centroids ใหม่จะแตกต่างจาก centroids เก่าแล้วไปที่ขั้นตอนที่ 3 มิฉะนั้นหยุดและนี่คือมันสวยมาก! จึงขอทำความเข้าใจนี้: เรารู้ว่าสิ่งที่ทำตามขั้นตอนที่ 1 และ 2 มีค่าเฉลี่ย สำหรับขั้นตอนที่ 2 ผมได้สีที่ด้านบน X จากภาพและใช้ค่าที่เป็น centroid ในคำอื่น ๆ ถ้าผมบอกว่าผมต้องการที่ 2 กลุ่มแล้วถ้าด้านบน 2 สี (สีที่มีพิกเซลมากที่สุด) จากภาพที่เป็น RGB: 100200130 และ 40,70,140 แล้วค่าเหล่านี้กลายเป็นเริ่มต้นของฉัน centroids 2 ขั้นตอนที่ 3a เป็นที่ที่เราจะ อ่านแต่ละพิกเซลในภาพและหาระยะห่างจาก 2 centroids ตอนนี้สิ่งที่เป็นระยะทางที่ ฉันได้ใช้ระยะทางแบบยุคลิด แต่คุณสามารถใช้ใด ๆ แต่จำไว้ว่าประเภทของระยะทางที่ใช้ที่จะส่งผลกระทบต่อผลการแบ่งส่วน นี้เป็นเพราะใช้ระยะนี้เราวัดว่าคล้ายสีพิกเซลในปัจจุบันคือการ centroid คลัสเตอร์ ที่จะเน้นจุดนี้ฉันได้ใช้ RGB และ HSV รูปแบบสี โดยใช้หมายเลขเดียวกันของกลุ่มเพื่อ RGB และ HSV จะผลิตที่แตกต่างกันผลการแบ่งส่วนขั้นตอนที่ 3b ย้ายพิกเซลกับกลุ่มใกล้เคียงที่สุด ดังนั้นถ้าเช่น 110215110 พิกเซลจะอยู่ใกล้เคียงกับยุคลิดระยะ centroid กลุ่มแรกและพิกเซล 50,80,160 จะใกล้เคียงกับการที่สองขั้นตอนที่ 4 เป็นที่ที่เราพบ centroids ใหม่สำหรับกลุ่ม ในตัวอย่างปัจจุบัน centroids ใหม่: +110 100 / 2,200 215 / 2,130 110/2 และ 40 50 / 2,70 80/2140 160/2 ใช่ centroids ใหม่จะพบว่าการใช้เฉลี่ยขั้นที่ 5 คือการตรวจสอบการบรรจบกัน หาก centroids ใหม่เช่นเดียวกับ centroids เก่าแล้วนั่นหมายความว่าข้อมูลภายใน centroids ยังไม่ได้ย้ายและกลุ่มมีเสถียรภาพ!
การแปล กรุณารอสักครู่..
![](//thimg.ilovetranslation.com/pic/loading_3.gif?v=b9814dd30c1d7c59_8619)